package com.forenet.controller.bill;
import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.itextpdf.text.Chunk;
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.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class OutUtil {
private Color black = new Color(0, 0, 0); // 黑色
private Color red = new Color(0, 255, 0, 0); // 红色
private int bold = Font.BOLD; // 粗体
private int normal = Font.NORMAL; // 正常字体
private int italic = Font.ITALIC; // 斜体
private float setting = 200; // 首行缩进参数
File file;// 将要返回的PDF文件
private Configuration configuration = null;
/**
* 构造方法
*/
public OutUtil() {
try {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @param sql 传过来的sql语句
* @param fileName 生成的文件名
* @param templatePath 模板路径
* @param TemplateFileName 模板文件名
*/
public File outWPH(String sql,String fileName,String TemplateFileName) {
//获取WebRoot物理路径
String PATH =OutUtil.class.getResource("/").toString().substring(6); //截取路径 注意此路径获得 项目编译后的路径 建议多测试下
File file = new File(fileName);
Map<String, Object> map = new HashMap<String, Object>();
// 构造列表循环数据存放在ArrayList集合中
List<Record> list = Db.find(sql);
int rowCount = 0;
if(TemplateFileName=="acctuserxls.ftl" || TemplateFileName=="acctorgxls.ftl"){
rowCount = list.size()+1;
}
map.put("list", list);
map.put("RowCount", rowCount);
Writer out = null;
try {
// 生成test.doc的word文件到某文件路径下
FileOutputStream fos = new FileOutputStream(file);
out = new OutputStreamWriter(fos, "UTF-8");
// 根据类路径获取模板
configuration.setDirectoryForTemplateLoading(new File(PATH));
Template t = configuration.getTemplate(TemplateFileName);
t.setEncoding("UTF-8");
//写入参数
t.process(map, out);
out.close();
System.out.println("导出"+fileName+"成功!");
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
/**
* 导出PDF
* @param head[] 为列头的参数
* @param cols 为列的参数填数据库参数名
* @param fileName 为文件名
* @param tableCells 为表列数
*/
public File outPDF(String[] heads, String[] cols, String sql, String fileName,int tableCells) {
Document document;
try {
document = createDoc(fileName + ".pdf");
// 打开文档
document.open(); // 文档里写入
// 生成N列表格 tableCells参数自己定义
PdfPTable table = new PdfPTable(tableCells);
Chunk chunk1 = new Chunk(convertChunkByChinese(fileName, 20, bold, black));
Paragraph par = new Paragraph();
par.add(chunk1);
par.setFirstLineIndent(setting); // 居中
document.add(par); // 新建章节
// 设置表格具体宽
table.setTotalWidth(90);
// 设置每一列所占的长度
table.setWidths(new float[] { 5f, 5f, 5f, 5f, 5f, 5f,5f});
for (int i = 0; i < heads.length; i++) {
table.addCell(convertParToChinese(heads[i], 14, bold, red));
}
document.add(new Paragraph("\n"));
List<Record> list = Db.find(sql);
Record record = null;
if (list != null) {
// 获取所有的记录 有多少条记录就创建多少行
for (int i = 0; i < list.size(); i++) {
// 得到所有的行 一个record就代表 一行
record = list.get(i);
// 在有所有的记录基础之上,便利传入进来的表头,再创建N行
for (int j = 0; j < cols.length; j++) {
// 把每一行的记录再次添加到表头下面 如果为空就为 "" 否则就为值
table.addCell(convertParToChinese(record.get(cols[j]) == null ? "" : record.get(cols[j]).toString(), 12, normal, red));
}
}
}
document.add(table);
Paragraph centerPar = convertParToChinese("剧中测试", 16, italic, black);
centerPar.setAlignment(Element.ALIGN_CENTER);
document.close();
System.out.println("导出"+fileName+".PDF成功");
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
@SuppressWarnings("hiding")
public Document createDoc(String filename) throws Exception {
// 新建document对象
// 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, 0, 0, 0, 0);
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
// 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
file = new File(filename);
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
PdfWriter.getInstance(document, fileOutputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return document;
}
/**
* 编码格式,添加防止中文不显示
* @param text
* @param fontsize
* @param fontStyle
* @param color
* @return graph
* @throws Exception
*/
public static Paragraph convertParToChinese(String text, int fontsize,int fontStyle, Color color) throws Exception {
BaseFont baseFontChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(baseFontChinese, fontsize, fontStyle);
Paragraph graph = new Paragraph(text, fontChinese);
return graph;
}
public Chunk convertChunkByChinese(String text, int fontsize, int fontStyle, Color color) throws Exception {
BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(baseFontChinese, fontsize, fontStyle);
Chunk chunk = new Chunk(text, fontChinese);
return chunk;
}
}