excel导出的另一种形式及word、ppt文件的导出
一、EXCEL
相信都有使用过用POI读取或导出excel文件的经历,很多时候都是在用POI的标准格式来输出文件。
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(); //生成第1页工作表
HSSFRow row = sheet.createRow(0); //第1行
HSSFCell cell1 = row.createCell(0); //第1列
cell1.setCellValue("1,1");
HSSFCell cell2 = row.createCell(1); //第2列
cell2.setCellValue("1,2");
HSSFCell cell3 = row.createCell(2); //第3列
cell3.setCellValue("1,3");
FileOutputStream out = new FileOutputStream(new File("E:\\excel\\demo.xls"));
wb.write(out);
这是一个简易的示范例子,这样的输出方式很简单,也很直观。
但是有时候我们会遇到一个比较复杂的excel模版格式,这时候使用这种方式就有些不够直观,代码的可读性也不是特别强,尤其是在时间要求很紧的时候,就有可能出现一些问题。
这时候我们可以注意一下office提供的一个方式——另存为html文件。
Excel会把文件保存为一个html文件,然后可以用浏览器打开,我们会发现这个文件的格式基本都已保存了。我们只需要在html代码中找到文件的基本结构——即一个<table>标签,将我们需要的数据填入相应的地方,保存起来就可以了。
将html代码中的其他部分删除,只保留<table>标签内的内容
使用输出字符串来输出excel文件:
StringBuffer s = new StringBuffer();
s.append("<table><tr>");
s.append("<td>1,1</td>");
s.append("<td>1,2</td>");
s.append("<td>1,3</td>");
s.append("</tr></table>");
FileOutputStream fileos = new FileOutputStream(new File("E:\\excel\\demo2.xls"));
PrintWriter out = new PrintWriter(fileos);
out.append(s);
out.flush();
out.close();
这个方法对于一些比较复杂的表格实用性较为强一些,但有些不够直观。
二、WORD
虽然很多时候都没有要求导出word文件的需求,但有时候确实会遇到这种要求,而像上文这样保存excel的方式也可以用于word文件的导出。
POIFSFileSystem wd = new POIFSFileSystem();
StringBuffer context = new StringBuffer();
context.append("<title>demo</title>");
context.append("<body>");
context.append("<p class=MsoNormal align=center style='text-align:center'><b style='mso-bidi-font-weight:");
context.append("normal'><span style='font-size:18.0pt;font-family:宋体;mso-ascii-font-family:");
context.append("Calibri;mso-hansi-font-family:Calibri'>系统月报</span></b><b");
context.append("style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-size:18.0pt'>201201<o:p></o:p></span></b></p>");
StringBuffer s = new StringBuffer();
s.append("<table border=1><tr>");
s.append("<td >1,1</td>");
s.append("<td>1,2</td>");
s.append("<td>1,3</td>");
s.append("</tr></table>");
context.append(s);
context.append("</body>");
context.append("</html>");
byte b[] = context.toString().getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
DirectoryEntry directory = wd.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
FileOutputStream ostream = new FileOutputStream("E:\\excel\\b.doc");
wd.writeFilesystem(ostream);
bais.close();
ostream.close();
文件中可以插入table表格,值得注意的是一定要为文件中的文本设定CSS格式,文档中的所有数据都有格式要求,这一点需要格外注意,在导出excel的时候也要注意格式。
三、PPT
POI也包含了导出PPT文件的操作和对象,主要包括在org.apache.poi.hslf包。
下面是一个简单的例子:
SlideShow ppt = new SlideShow();
Slide slide = ppt.createSlide();
// 添加标题
TextBox title = slide.addTitle();
RichTextRun titleRun = title.getTextRun().getRichTextRuns()[0];
titleRun.setFontColor(Color.RED);
title.setText("ppt测试");
// 添加文本框
TextBox txt = new TextBox();
RichTextRun richTextRun = txt.getTextRun().getRichTextRuns()[0];
richTextRun.setFontColor(Color.BLUE);
// setText参数字符串可以包含回车、换行符,但是最后一行不能以\r\n结尾,否则设置的格式没有效果(v3.5)
richTextRun.setText("这里可以换行\r\n第二行文本");
txt.setAnchor(new java.awt.Rectangle(50, 150, 400, 400));
slide.addShape(txt);
// 新一页
slide = ppt.createSlide();
// 列表数据封装成String矩阵
String[][] datas = { { "序号", "姓名", "年龄" }, { "1", "张三", "30" },{ "2", "李四", "27" }, };
// 3行3列表格
Table table = new Table(3, 3);
for (int i = 0; i < datas.length; i++) {
for (int j = 0; j < datas.length; j++) {
TableCell cell = table.getCell(i, j);
RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName("宋体");
rt.setFontSize(12);
cell.setVerticalAlignment(TextBox.AnchorMiddle);
cell.setHorizontalAlignment(TextBox.AlignCenter);
cell.setText(datas[j]);
if (i == 0) {// 首行背景设置为灰色
cell.setFillColor(Color.GRAY);
}
}
}
Line border = table.createBorder();
border.setLineColor(Color.black);
border.setLineWidth(2.0);
table.setAllBorders(border);
slide.addShape(table);
table.moveTo(160,260);
// 设置幻灯片大小
ppt.setPageSize(new Dimension(700, 600));
SlideMaster master = ppt.getSlidesMasters()[0];
// 设置背景图片,位置
int picIndex = ppt.addPicture(new File("E:\\ppt\\background.png"),Picture.PNG);
Picture background = new Picture(picIndex);
background.setAnchor(new java.awt.Rectangle(0, 0,ppt.getPageSize().width, ppt.getPageSize().height));
master.addShape(background);
File file = new File("E:\\ppt\\demo.ppt");
FileOutputStream out = new FileOutputStream(file);
ppt.write(out);
out.close();