pdf图表导出总结,同时可以应用到excel中
来源:互联网 发布:linux安全加固方案 编辑:程序博客网 时间:2024/06/05 05:54
1,jsp中图形报表的实现,通过返回svg格式到action中
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Highcharts</title>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/highcharts.js"></script>
<script type="text/javascript" src="js/exporting.js"></script>
<script type="text/javascript">
$(function () {
Highcharts.wrap(Highcharts.Chart.prototype, 'getSVG', function (proceed) {
return proceed.call(this)
.replace(
/(fill|stroke)="rgba\(([ 0-9]+,[ 0-9]+,[ 0-9]+),([ 0-9\.]+)\)"/g,
'$1="rgb($2)" $1-opacity="$3"'
);
});
$('#container').highcharts({
chart: {
type: 'line'
},
title: {
text: 'Monthly Average Temperature'
},
subtitle: {
text: 'Source: WorldClimate.com'
},
credits:{
enabled:false//不显示highCharts版权信息
},
xAxis: {
categories: ['', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
title: {
text: '百分比 (%)'
}
},
plotOptions: {
line: {
dataLabels: {
enabled: true
},
enableMouseTracking: false
}
},
series: [{
name: 'A任务',
data: [7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
}, {
name: 'B任务',
data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
}]
});
$("#btn_word").on("click", function() {
var chart_line = $("#container").highcharts();
<%-- var chart_pie = $("#container_pie").highcharts();--%>
var svg_line = chart_line.getSVG();
<%-- var svg_pie = chart_pie.getSVG();--%>
var svg = svg_line
<%-- +"_"+svg_pie;--%>
$("#svg").val(svg);
$("#form1").prop("action", "export.do?type=word").submit();
});
$("#btn_pdf").on("click", function() {
var chart_line = $("#container").highcharts();
<%-- var chart_pie = $("#container_pie").highcharts();--%>
var svg_line = chart_line.getSVG();
<%-- var svg_pie = chart_pie.getSVG();--%>
var svg = svg_line
<%-- + "_" + svg_pie;--%>
$("#svg").val(svg);
$("#form1").prop("action", "export.do?type=pdf").submit();
});
});
</script>
</head>
<form id="form1" action="export.do" method="post">
<div>
<input type="hidden" name="svg" id="svg" />
<input id="btn_word" type="button" value="导出word" />
<input id="btn_pdf" type="button" value="导出pdf" />
</div>
</form>
<body>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
</body>
</html>
2,action的图片导出
package action;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.batik.transcoder.TranscoderException;
import util.ItextManager;
import util.PdfManage;
import util.PdfManageImp;
import util.SvgPngConverter;
public class ExportAction extends HttpServlet {
@Override
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
HttpServletRequest request=(HttpServletRequest) arg0;
HttpServletResponse response=(HttpServletResponse) arg1;
try {
exportWord(request,response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String exportWord(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String type = request.getParameter("type");
request.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream; charset=UTF-8");
if ("word".equals(type)) {
response.setHeader("content-disposition", "attachment;filename="
+ new SimpleDateFormat("yyyyMMddHH:mm:ss").format(new Date()) + ".doc");
} else if ("pdf".equals(type)) {
response.setHeader("content-disposition", "attachment;filename="
+ new SimpleDateFormat("yyyyMMddHH:mm:ss").format(new Date()) + ".pdf");
}
String svgCode = request.getParameter("svg");
String svg[] = svgCode.split("_");
String path[] = new String[svg.length];
OutputStream out = response.getOutputStream();
// ItextManager tm = ItextManager.getInstance();
PdfManageImp pm=new PdfManageImp();
List<String> imageList = new ArrayList<String>();
if (svg != null) {
for (int k = 0; k < svg.length; k++) {
String picName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".png";
path[k] = request.getSession().getServletContext().getRealPath("/upload/" + picName);
imageList.add(path[k]);
SvgPngConverter.convertToPng(svg[k], path[k]);
}
}
// tm.createRtfContext(imageList, out, type);
pm.doExport(imageList, out, type);
out.flush();
out.close();
return null;
}
}
3,其中涉及到的格式转换,还有pdf工具类
①具体的实现类
,package util;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.util.List;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class PdfManageImp extends PdfManage {
public void doExport(List<String> imglist, OutputStream out, String type) throws DocumentException, MalformedURLException, IOException{
Document doc = new Document();
if(type.equals("pdf")){
PdfWriter.getInstance(doc, out);
}
doc.open();
Image img = null;
// 图片
img = Image.getInstance(imglist.get(0));
float heigth = img.getHeight();
float width = img.getWidth();
int percent = getPercent2(heigth, width);
img.setAlignment(Image.MIDDLE);
img.scalePercent(percent + 3);// 表示是原来图像的比例;
doc.add(img);
PdfPTable table=createTable(3);
table.addCell(createCell("户口本", textfont, 2, 2));
table.addCell(createCell("户口本2", textfont, 1, 1));
table.addCell(createCell("你还好么", textfont, 3, 1));
doc.add(table);
doc.close();
}
}
②,pdfmanager工具类
package util;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.util.List;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public abstract class PdfManage {
protected static Font headfont;
protected static Font keyfont;
protected static Font textfont;
static {
BaseFont bfChinese;
try {
bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
headfont = new Font(bfChinese, 10, Font.BOLD);
keyfont = new Font(bfChinese, 8, Font.BOLD);
textfont = new Font(bfChinese, 8, Font.NORMAL);
} catch (Exception e) {
e.printStackTrace();
}
}
public abstract void doExport(List<String> imglist,java.io.OutputStream out,String type) throws Exception;
/*throws DocumentException, MalformedURLException, IOException{
Document doc = new Document();
if(type.equals("pdf")){
PdfWriter.getInstance(doc, out);
}
doc.open();
Image img = null;
// 图片
img = Image.getInstance(imglist.get(0));
float heigth = img.getHeight();
float width = img.getWidth();
int percent = getPercent2(heigth, width);
img.setAlignment(Image.MIDDLE);
img.scalePercent(percent + 3);// 表示是原来图像的比例;
doc.add(img);
PdfPTable table=createTable(3);
table.addCell(createCell("户口本", textfont, 2, 2));
table.addCell(createCell("户口本2", textfont, 1, 1));
table.addCell(createCell("你还好么", textfont, 3, 1));
doc.add(table);
doc.close();*/
public PdfPCell createCell(String value,Font font,int cols,int rows){
PdfPCell cell=new PdfPCell();
cell.setPhrase(new Phrase(value, font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setColspan(cols);
cell.setRowspan(rows);
return cell;
}
public PdfPTable createTable(int colNumber) {
PdfPTable table = new PdfPTable(colNumber);
try {
table.setTotalWidth(520);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}
public static int getPercent2(float h, float w) {
int p = 0;
float p2 = 0.0f;
p2 = 530 / w * 100;
p = Math.round(p2);
return p;
}
}
③,svg转换成png图片的工具类
package util;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
/**
*@Description: 将svg转换为png格式的图片
*/
public class SvgPngConverter {
/**
*@Description: 将svg字符串转换为png
*@Author:
*@param svgCode svg代码
*@param pngFilePath 保存的路径
*@throws IOException io异常
*@throws TranscoderException svg代码异常
*/
public static void convertToPng(String svgCode,String pngFilePath) throws IOException,TranscoderException{
File file = new File (pngFilePath);
FileOutputStream outputStream = null;
try {
file.createNewFile ();
outputStream = new FileOutputStream (file);
convertToPng (svgCode, outputStream);
} finally {
if (outputStream != null) {
try {
outputStream.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
}
/**
*@Description: 将svgCode转换成png文件,直接输出到流中
*@param svgCode svg代码
*@param outputStream 输出流
*@throws TranscoderException 异常
*@throws IOException io异常
*/
public static void convertToPng(String svgCode,OutputStream outputStream) throws TranscoderException,IOException{
try {
byte[] bytes = svgCode.getBytes ("UTF-8");
PNGTranscoder t = new PNGTranscoder ();
TranscoderInput input = new TranscoderInput (new ByteArrayInputStream (bytes));
TranscoderOutput output = new TranscoderOutput (outputStream);
t.transcode (input, output);
outputStream.flush ();
} finally {
if (outputStream != null) {
try {
outputStream.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
}
}
- pdf图表导出总结,同时可以应用到excel中
- 实战应用导出gridview到excel word pdf保留格式
- 实战应用导出gridview到excel word pdf保留格式
- 把表的数据导出到EXCEL,同时还可以打印
- Excel vba从excel中导出表格数据和图表到新word文档
- phpExcel导出到Excel和pdf
- C#导出到Excel或PDF
- c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据
- c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据
- winform DataGridView导出数据到Excel中,可以导出当前页和全部数据
- C#导出数据到Excel中方法总结
- C++ Builder 将数据库数据导出到Excel中总结
- 个人总结thinkphp中导出数据到excel表格
- iText导出pdf,poi导出excel并下载到客户端
- iText导出pdf,poi导出excel并下载到客户端
- echarts图表导出excel
- js 将图表导入到Excel 中
- datagrid数据及echarts图表导出到Excel
- Ubuntu64位系统运行32位程序
- Android 5.x OTA Update官方文档(一,关于OTA 升级)
- libuv的编译
- TCP、UDP、IP报头结构体分析
- execl 导出文件
- pdf图表导出总结,同时可以应用到excel中
- 马一鸣AA小助手-1
- android studio 的神器--bitmap
- spark-parquet列存储之:文件存储细节之:RowWriteSupport和RecordWriter
- 基于Detours劫持360启动
- Getting Started with TLM-2.0
- oracle connect by start with
- dojo上传功能有用的文章
- Lua学习(六)-----函数