java PDF导出
来源:互联网 发布:java抽象工厂 编辑:程序博客网 时间:2024/06/14 04:52
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
<!-- 指标查询的tab页内容 start-->
<div class="tab_contents">
<!-- 搜索条件面板 -->
<div class="search_box_panel">
<table class="search-box-table">
<tr>
<th width='40px'></th>
<th>年份:</th>
<th>
<select id="beginYear" name="" class="ui-text">
</select>
</th>
<th>至</th>
<th>
<select id="endYear" name="" class="ui-text">
</select>
</th>
<th width='55%'></th>
<th><button id="search_for_index_btn" class="ui-btn ui-btn-primary">查询</button></th>
<th><button class="ui-btn ui-btn-primary" onclick="expPdf();">导出PDF</button></th>
<th><button class="ui-btn ui-btn-primary" onclick="expExl();">导出Excel</button></th>
</tr>
</table>
</div>
<!-- 搜索结果面板 -->
<div id="search_result_panel" class="search_result_panel">
<table id="index_query_table">
<thead>
<tr>
<th rowspan="2">序号</th>
<th rowspan="2">年份</th>
<th colspan="4">节电量(万kWh)</th>
<th colspan="4">节电力(万kW)</th>
<th rowspan="2">状态</th>
</tr>
<tr>
<th>预指标值</th>
<th>确认值</th>
<th>承担值</th>
<th>指标值</th>
<th>预指标值</th>
<th>确认值</th>
<th>承担值</th>
<th>指标值</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<!-- 指标查询的tab页内容 end-->
function expPdf(){
var rs={"data":[]};
$("#index_query_table tr").each(function(index){
var temp=[];
$(this).children("td").each(function(i){
temp.push($(this).text());
});
if(temp.length!=0)
rs.data.push(temp);
});
window.location.href="common/ExpExlController/expPdf?rs="+JSON.stringify(rs)+"&&fileName=dutyAnalyze.pdf";
}
@SuppressWarnings("unchecked")
@RequestMapping(value = "/expPdf")
@ResponseBody
public void expPdf(HttpServletRequest request,
HttpServletResponse response) {
LOGGER.info("here expPdf.");
try {
JSONObject json = JSONObject.parseObject(request.getParameter("rs"));
List<List<Object>> data = (List<List<Object>>) json.get("data");
commonService.expPdf(request,response,data);
} catch (Throwable ex) {
MyHttpServletUtil.sendErrorBadRequest(response);
}
}
@Override
public void expPdf(HttpServletRequest request,
HttpServletResponse response, List<List<Object>> data) {
ByteArrayOutputStream baos = null;
OutputStream out = null;
try {
Rectangle rect = new Rectangle(PageSize.A4);// 设置页面大小
rect.setBackgroundColor(BaseColor.WHITE);// 页面背景色
Document document = new Document(rect);
baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
//设置字体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 8, Font.NORMAL);// 创建字体,设置family,size,style,还可以设置color
Font titleChinese = new Font(bfChinese, 16, Font.BOLD);
Font subBoldFontChinese = new Font(bfChinese, 8, Font.BOLD);
BaseColor gray = new BaseColor(251, 251, 251);
document.open();
Paragraph title = new Paragraph("江西省节约电力电量指标", titleChinese);// 设置标题
title.setAlignment(Element.ALIGN_CENTER);// 设置对齐方式
title.setLeading(20f);// 设置行间距
document.add(title);
float[] widths = {10f,10f,10f,10f,10f,10f,10f,10f,10f,10f,10f};// 设置表格的列宽和列数 默认是4列
PdfPTable table = new PdfPTable(widths);// 建立一个pdf表格
table.setSpacingBefore(10f);
table.setWidthPercentage(100);// 设置表格宽度为100%
PdfPCell cell = null;
cell = new PdfPCell(new Paragraph("序号",subBoldFontChinese));
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setRowspan(2);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("年份",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setRowspan(2);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("节电量(万kWh)",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(4);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("节电力(万kW)",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(4);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("状态",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setRowspan(2);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("预指标值",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("确认值",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("承担值",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("指标值",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("预指标值",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("确认值",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("承担值",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("指标值",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5f);
table.addCell(cell);
for (List<Object> each : data) {
for (Object obj : each) {
cell = new PdfPCell(new Paragraph(obj.toString(),
fontChinese));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
if(obj.toString().contains("没有数据")) {
cell.setColspan(11);
}
cell.setPadding(5f);
table.addCell(cell);
}
}
document.add(table);
document.close();
String fileName = XssStringFilter.crlfFilter(request.getParameter("fileName"));
response.setHeader("Expires", "0");
response.setHeader("Content-Disposition",
"attachment; filename=\""+fileName+"\"");
response.setContentType("application/pdf;charset=utf-8");
response.setContentLength(baos.size());
out = response.getOutputStream();
baos.writeTo(out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
}
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
<!-- 指标查询的tab页内容 start-->
<div class="tab_contents">
<!-- 搜索条件面板 -->
<div class="search_box_panel">
<table class="search-box-table">
<tr>
<th width='40px'></th>
<th>年份:</th>
<th>
<select id="beginYear" name="" class="ui-text">
</select>
</th>
<th>至</th>
<th>
<select id="endYear" name="" class="ui-text">
</select>
</th>
<th width='55%'></th>
<th><button id="search_for_index_btn" class="ui-btn ui-btn-primary">查询</button></th>
<th><button class="ui-btn ui-btn-primary" onclick="expPdf();">导出PDF</button></th>
<th><button class="ui-btn ui-btn-primary" onclick="expExl();">导出Excel</button></th>
</tr>
</table>
</div>
<!-- 搜索结果面板 -->
<div id="search_result_panel" class="search_result_panel">
<table id="index_query_table">
<thead>
<tr>
<th rowspan="2">序号</th>
<th rowspan="2">年份</th>
<th colspan="4">节电量(万kWh)</th>
<th colspan="4">节电力(万kW)</th>
<th rowspan="2">状态</th>
</tr>
<tr>
<th>预指标值</th>
<th>确认值</th>
<th>承担值</th>
<th>指标值</th>
<th>预指标值</th>
<th>确认值</th>
<th>承担值</th>
<th>指标值</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<!-- 指标查询的tab页内容 end-->
function expPdf(){
var rs={"data":[]};
$("#index_query_table tr").each(function(index){
var temp=[];
$(this).children("td").each(function(i){
temp.push($(this).text());
});
if(temp.length!=0)
rs.data.push(temp);
});
window.location.href="common/ExpExlController/expPdf?rs="+JSON.stringify(rs)+"&&fileName=dutyAnalyze.pdf";
}
@SuppressWarnings("unchecked")
@RequestMapping(value = "/expPdf")
@ResponseBody
public void expPdf(HttpServletRequest request,
HttpServletResponse response) {
LOGGER.info("here expPdf.");
try {
JSONObject json = JSONObject.parseObject(request.getParameter("rs"));
List<List<Object>> data = (List<List<Object>>) json.get("data");
commonService.expPdf(request,response,data);
} catch (Throwable ex) {
MyHttpServletUtil.sendErrorBadRequest(response);
}
}
@Override
public void expPdf(HttpServletRequest request,
HttpServletResponse response, List<List<Object>> data) {
ByteArrayOutputStream baos = null;
OutputStream out = null;
try {
Rectangle rect = new Rectangle(PageSize.A4);// 设置页面大小
rect.setBackgroundColor(BaseColor.WHITE);// 页面背景色
Document document = new Document(rect);
baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
//设置字体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 8, Font.NORMAL);// 创建字体,设置family,size,style,还可以设置color
Font titleChinese = new Font(bfChinese, 16, Font.BOLD);
Font subBoldFontChinese = new Font(bfChinese, 8, Font.BOLD);
BaseColor gray = new BaseColor(251, 251, 251);
document.open();
Paragraph title = new Paragraph("江西省节约电力电量指标", titleChinese);// 设置标题
title.setAlignment(Element.ALIGN_CENTER);// 设置对齐方式
title.setLeading(20f);// 设置行间距
document.add(title);
float[] widths = {10f,10f,10f,10f,10f,10f,10f,10f,10f,10f,10f};// 设置表格的列宽和列数 默认是4列
PdfPTable table = new PdfPTable(widths);// 建立一个pdf表格
table.setSpacingBefore(10f);
table.setWidthPercentage(100);// 设置表格宽度为100%
PdfPCell cell = null;
cell = new PdfPCell(new Paragraph("序号",subBoldFontChinese));
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setRowspan(2);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("年份",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setRowspan(2);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("节电量(万kWh)",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(4);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("节电力(万kW)",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(4);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("状态",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setRowspan(2);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("预指标值",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("确认值",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("承担值",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("指标值",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("预指标值",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("确认值",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("承担值",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("指标值",subBoldFontChinese));//
cell.setBackgroundColor(gray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5f);
table.addCell(cell);
for (List<Object> each : data) {
for (Object obj : each) {
cell = new PdfPCell(new Paragraph(obj.toString(),
fontChinese));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
if(obj.toString().contains("没有数据")) {
cell.setColspan(11);
}
cell.setPadding(5f);
table.addCell(cell);
}
}
document.add(table);
document.close();
String fileName = XssStringFilter.crlfFilter(request.getParameter("fileName"));
response.setHeader("Expires", "0");
response.setHeader("Content-Disposition",
"attachment; filename=\""+fileName+"\"");
response.setContentType("application/pdf;charset=utf-8");
response.setContentLength(baos.size());
out = response.getOutputStream();
baos.writeTo(out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
}
阅读全文
0 0
- JAVA 导出PDF文件
- Java实现Pdf导出
- java导出pdf
- java导出pdf
- java PDF文件导出
- java 导出PDF
- java导出pdf
- java 导出pdf
- java导出PDF文件
- java导出pdf
- Java导出PDF文件
- Java导出Pdf范例
- java导出pdf文档
- java导出pdf
- java导出pdf
- java导出pdf
- java导出pdf
- Java导出pdf表格
- 转载Python 的zip函数
- 出现Error:no csrf hash code!怎么办 给你支招
- 一致性 hash 算法( consistent hashing )
- Linux 下 make 构建过程
- 学习率 Learning Rate
- java PDF导出
- phpstorm配置MonokaiSublime主题
- 1064. 朋友数(20)--Python
- 心理辅导平台设计
- 【安卓小笔记】v7包下SearchView的基本使用
- 【金融财经】金融市场一周简报(2017-12-01)
- 公众号每次调用接口时,可能获得正确或错误的返回码,开发者可以根据返回码信息调试接口,排查错误。 全局返回码说明如下:
- 织梦dede后台发布文章后后台显示空白页面解决办法
- 数字排序 o(n)时间 o(1)空间