import com.lowagie.text.*; import com.lowagie.text.Font; import com.lowagie.text.rtf.RtfWriter2; import com.lowagie.text.rtf.style.RtfFont; import com.test.action.monitor.service.CommonService; import org.springframework.beans.factory.annotation.Autowired; import java.awt.*; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; public class exportTest { /* iText是一个开源的PDF、Word工具,小巧而又便捷。 官方网站是:http://www.lowagie.com/iText/ 官方的解释:iText is a library that allows you to generate PDF files . the fly 最新版本为:2.1.7 下载地址是:http://nchc.dl.sourceforge.net/sourceforge/itext/iText-2.1.7.jar */ public String exportWordFileName; public String exportWordDocPath; public FileInputStream inputStream; @Autowired private CommonService commonService; /** * 综合报表统计导出Word * * @author QC */ public String exportPDF() { try { /** 创建Document对象(Word文档)*/ Document doc = new Document(PageSize.A4); String exportname = "测试报表"; String webTempDir = "/test/app/config"; String filePath = webTempDir + UUID.randomUUID().toString().replace("-", "") + ".doc"; FileOutputStream fileOut = new FileOutputStream(filePath); RtfWriter2.getInstance(doc, fileOut); doc.open(); RtfFont reportTitleFont = new RtfFont("仿宋_GB2312", 16, Font.BOLD, Color.BLACK); RtfFont titleFont = new RtfFont("仿宋_GB2312", 10, Font.BOLD, Color.BLACK); RtfFont contextFont = new RtfFont("仿宋_GB2312", 10, Font.NORMAL, Color.BLACK); Paragraph pt = new Paragraph(exportname, reportTitleFont);//设置字体样式 pt.setAlignment(Element.ALIGN_CENTER);//设置文字居中 0靠左 1,居中 2,靠右 doc.add(pt); //服务器统计Demo------------------------------------------------------------------------------------------------------------------------- List<Map<String, Object>> listmap = this.getServerList();//待插入的数据 //创建table Table tableServer = new Table(10); int[] withsS = {18, 18, 8, 10, 8, 8, 10, 8, 10, 10}; tableServer.setWidths(withsS); tableServer.setWidth(100); tableServer.setAlignment(Element.ALIGN_CENTER); tableServer.setAutoFillEmptyCells(true); doc.add(new Paragraph("")); Paragraph serverPt = new Paragraph("二、服务器运行指标", titleFont);//设置字体样式 serverPt.setAlignment(Element.ALIGN_LEFT);//设置文字居中 0靠左 1,居中 2,靠右 doc.add(serverPt); /**表格标题*/ Cell[] servercellHeaders = new Cell[10]; servercellHeaders[0] = new Cell(new Phrase("服务器", titleFont)); servercellHeaders[0].setColspan(10); servercellHeaders[0].setHorizontalAlignment(Element.ALIGN_CENTER); servercellHeaders[0].setVerticalAlignment(Element.ALIGN_MIDDLE); tableServer.addCell(servercellHeaders[0]); /** 第三行(表格)*/ Cell[] servercellHeaders2 = new Cell[12]; servercellHeaders2[0] = new Cell(new Phrase("名称", titleFont)); servercellHeaders2[0].setColspan(2); servercellHeaders2[0].setRowspan(2); servercellHeaders2[1] = new Cell(new Phrase("CPU使用率", titleFont)); servercellHeaders2[1].setColspan(3); servercellHeaders2[2] = new Cell(new Phrase("内存使用率", titleFont)); servercellHeaders2[2].setColspan(3); servercellHeaders2[3] = new Cell(new Phrase("逻辑分区使用率", titleFont)); servercellHeaders2[3].setColspan(2); servercellHeaders2[4] = new Cell(new Phrase("最高值", titleFont)); servercellHeaders2[5] = new Cell(new Phrase("最高值时间", titleFont)); servercellHeaders2[6] = new Cell(new Phrase("平均值", titleFont)); servercellHeaders2[7] = new Cell(new Phrase("最高值", titleFont)); servercellHeaders2[8] = new Cell(new Phrase("最高值时间", titleFont)); servercellHeaders2[9] = new Cell(new Phrase("平均值", titleFont)); servercellHeaders2[10] = new Cell(new Phrase("分区名称", titleFont)); servercellHeaders2[11] = new Cell(new Phrase("分区使用率", titleFont)); for (int i = 0; i < 12; i++) { servercellHeaders2[i].setHorizontalAlignment(Element.ALIGN_CENTER); servercellHeaders2[i].setVerticalAlignment(Element.ALIGN_MIDDLE); tableServer.addCell(servercellHeaders2[i]); } if (listmap != null && listmap.size() > 0) { int num = 0; String paramname = ""; String bid = ""; Map<String, Object> nummap = new HashMap(); for (int i = 0; i < listmap.size(); i++) { Map<String, Object> m = listmap.get(i); String param1 = m.get("PARAM1") != null ? m.get("PARAM1").toString() : ""; String setid = m.get("SETID") != null ? m.get("SETID").toString() : ""; if (!paramname.equals(param1)) { if (i > 0) { nummap.put(bid, num + "@-@" + paramname); } paramname = param1; num = 1; bid = setid; } else { num++; nummap.put(bid, num + "@-@" + paramname); } } int prows = 0; paramname = ""; for (int i = 0; i < listmap.size(); i++) { Map<String, Object> m = listmap.get(i); String param1 = m.get("PARAM1") != null ? m.get("PARAM1").toString() : ""; String setid = m.get("SETID") != null ? m.get("SETID").toString() : ""; bid = setid; if (nummap.containsKey(bid)) { String[] nstr = nummap.get(bid).toString().split("@-@"); num = Integer.parseInt(nstr[0]); paramname = nstr[1]; } else num = 1; int bc = 0; Cell[] servercellContent = new Cell[10]; if (num > 1 && prows == 0 && paramname.equals(param1)) {//合并第一行 servercellContent[0] = new Cell(new Phrase(m.get("PARAM1") != null ? m.get("PARAM1").toString() : "", contextFont)); servercellContent[0].setRowspan(num); prows = num; prows--; } else { if (num == 1 && prows == 0 && paramname.equals(param1)) {//不合并一行 servercellContent[0] = new Cell(new Phrase(m.get("PARAM1") != null ? m.get("PARAM1").toString() : "", contextFont)); bc = 0; } else { if (num == 1 && prows > 0 && paramname.equals(param1)) {//合并其它行 bc = 1; prows--; } } } servercellContent[1] = new Cell(new Phrase(m.get("NAME") != null ? m.get("NAME").toString() : "", contextFont)); servercellContent[2] = new Cell(new Phrase(m.get("MAXVAL") != null ? m.get("MAXVAL").toString() : "", contextFont)); servercellContent[3] = new Cell(new Phrase(m.get("RTIME") != null ? m.get("RTIME").toString() : "", contextFont)); servercellContent[4] = new Cell(new Phrase(m.get("AVGVAL") != null ? m.get("AVGVAL").toString() : "", contextFont)); servercellContent[5] = new Cell(new Phrase(m.get("MAXMEMUSAGE") != null ? m.get("MAXMEMUSAGE").toString() : "", contextFont)); servercellContent[6] = new Cell(new Phrase(m.get("MEMTIME") != null ? m.get("MEMTIME").toString() : "", contextFont)); servercellContent[7] = new Cell(new Phrase(m.get("AVGMEM") != null ? m.get("AVGMEM").toString() : "", contextFont)); servercellContent[8] = new Cell(new Phrase(m.get("PARAM2") != null ? m.get("PARAM2").toString() : "", contextFont)); servercellContent[9] = new Cell(new Phrase(m.get("PARAM4") != null ? m.get("PARAM4").toString() : "", contextFont)); for (int j = bc; j < 10; j++) { servercellContent[j].setHorizontalAlignment(Element.ALIGN_CENTER); servercellContent[j].setVerticalAlignment(Element.ALIGN_MIDDLE); tableServer.addCell(servercellContent[j]); } } } doc.add(tableServer); doc.close(); exportWordDocPath = filePath; exportWordFileName = exportname; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return "redirectDownloadAction"; } public List<Map<String, Object>> getServerList() throws Exception { String sql = "select r.id setid,r.param1,r.param2,r.param3,r.param4,r.param5,r.field_name name,r.order_no,r.field_value devid from report r "; List<Map<String, Object>> list = this.commonService.queryMapList(sql); return list; } }
Java导出Word文档使用iText操作
最新推荐文章于 2023-03-28 14:43:37 发布