在平常的開發中我們常常遇到不僅僅只是導出excel報表的情況。有時候也需要導出pdf或者CSV報表。其實原理都差不多。剛開始本來不打算也這篇博客介紹這個的。感覺這篇博客和前面的博客有點雷同。原理基本都一樣。但想了想。有時候可能有些童鞋遇到這樣的需求會無從下手。所以還是記錄下來。幫助一下那些需要這個需求的童鞋。如果你對前面幾篇博客的原理都搞明白了。這篇博客你完全可以不看了。僅僅只是代碼的實現不同而已。好了。下面我們來看一下需求吧。
這個圖就是我們的需求
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ?就像你看到的一樣。我們的需求就是列表內容是從數據庫中讀出來的。而我們想把從數據庫得到的這個列表導出pdf、csv、excel報表。也不多說了。看代碼吧:
[java]?view plaincopy print?
<pre?code_snippet_id="630890"?snippet_file_name="blog_20150329_1_746209"?name="code"?class="java">package?com.bzu.csh;?? ?? import?java.io.ByteArrayOutputStream;?? import?java.io.File;?? import?java.io.FileOutputStream;?? import?java.io.OutputStream;?? import?java.util.ArrayList;?? import?java.util.List;?? ?? import?javax.servlet.http.HttpServletRequest;?? import?javax.servlet.http.HttpServletResponse;?? ?? import?jxl.Workbook;?? import?jxl.write.Label;?? import?jxl.write.WritableFont;?? import?jxl.write.WritableSheet;?? import?jxl.write.WritableWorkbook;?? ?? import?org.apache.struts2.ServletActionContext;?? ?? import?com.lowagie.text.Document;?? import?com.lowagie.text.Element;?? import?com.lowagie.text.Font;?? import?com.lowagie.text.PageSize;?? import?com.lowagie.text.Paragraph;?? import?com.lowagie.text.pdf.PdfPTable;?? import?com.lowagie.text.pdf.PdfWriter;?? import?com.opensymphony.xwork2.Action;?? ?? public?class?downloadAction?implements?Action?{?? ?? ????private?String?downType;?? ?? ????public?String?getDownType()?{?? ????????return?downType;?? ????}?? ?? ????public?void?setDownType(String?downType)?{?? ????????this.downType?=?downType;?? ????}?? ?? ????public?String?execute()?{?? ?????????? ????????HttpServletRequest?request?=?ServletActionContext.getRequest();?? ?????????? ?????????? ????????List<Person>?list?=?new?ArrayList<Person>();?? ????????for?(int?i?=?1;?i?<?6;?i++)?{?? ????????????Person?person?=?new?Person();?? ????????????person.setId(String.valueOf(i));?? ????????????person.setName(String.valueOf((char)?(i?+?64)));?? ????????????person.setAge(i?+?20);?? ????????????person.setSex("man");?? ????????????list.add(person);?? ????????}?? ????????OutputStream?os?=?null;?? ????????String?fname?=?"personlist";?? ????????if?("PDF".equals(downType))?{?? ????????????try?{?? ?????????????? ?????????????? ????????????????FileOutputStream?out?=?new?FileOutputStream("d://a.pdf");?? ????????????????Document?document?=?new?Document(PageSize.A4,?50,?50,?50,?50);?? ?????????????? ?????????????? ?????????????? ????????????????ByteArrayOutputStream?baos?=?new?ByteArrayOutputStream();?? ?? ????????????????PdfWriter.getInstance(document,?out);?? ????????????????document.open();?? ????????????????int?cols?=?list.size();?? ?????????????????? ????????????????PdfPTable?table?=?new?PdfPTable(4);?? ?????????????????? ????????????????table.setTotalWidth(500);?? ?????????????????? ????????????????table.setLockedWidth(true);?? ?????????????????? ????????????????Font?thfont?=?new?Font();?? ?????????????????? ????????????????thfont.setSize(7);?? ?????????????????? ????????????????thfont.setStyle(Font.BOLD);?? ????????????????Font?tdfont?=?new?Font();?? ????????????????tdfont.setSize(7);?? ????????????????tdfont.setStyle(Font.NORMAL);?? ?????????????????? ????????????????table.setHorizontalAlignment(Element.ALIGN_MIDDLE);?? ?????????????????? ????????????????table.addCell(new?Paragraph("id",?thfont));?? ????????????????table.addCell(new?Paragraph("name",?thfont));?? ????????????????table.addCell(new?Paragraph("sex",?thfont));?? ????????????????table.addCell(new?Paragraph("age",?thfont));?? ?????????????????? ????????????????for?(int?i?=?0;?i?<?list.size();?i++)?{?? ????????????????????Person?p?=?(Person)?list.get(i);?? ????????????????????table.addCell(new?Paragraph(p.getId(),?tdfont));?? ????????????????????table.addCell(new?Paragraph(p.getName(),?tdfont));?? ????????????????????table.addCell(new?Paragraph(p.getSex(),?tdfont));?? ????????????????????table.addCell(new?Paragraph(String.valueOf(p.getAge()),?? ????????????????????????????tdfont));?? ????????????????}?? ????????????????document.add(table);?? ????????????????document.close();?? ?????????????? ????????????????baos.close();?? ????????????}?catch?(Exception?e)?{?? ????????????????e.printStackTrace();?? ????????????}?? ????????}?else?if?("CSV".equals(downType))?{?? ?????????? ?????????????? ?????????????? ?????????????? ?????????????? ?????????????? ????????????FileOutputStream?out?;?? ????????????String?sep?=?",";?? ????????????try?{?? ????????????????out?=?new?FileOutputStream(new?File("d://a.cvs"));?? ?????????????????? ????????????????out.write("id".getBytes());?? ????????????????out.write(sep.getBytes());?? ????????????????out.write("name".getBytes());?? ????????????????out.write(sep.getBytes());?? ????????????????out.write("sex".getBytes());?? ????????????????out.write(sep.getBytes());?? ????????????????out.write("age".getBytes());?? ????????????????out.write(sep.getBytes());?? ????????????????out.write(System.getProperty("line.separator").getBytes());?? ????????????????for?(int?i?=?0;?i?<?list.size();?i++)?{?? ????????????????????Person?p?=?(Person)?list.get(i);?? ????????????????????out.write(p.getId().getBytes());?? ????????????????????out.write((sep?+?"/t").getBytes());?? ????????????????????out.write(p.getName().getBytes());?? ????????????????????out.write((sep?+?"/t").getBytes());?? ????????????????????out.write(p.getSex().getBytes());?? ????????????????????out.write((sep?+?"/t").getBytes());?? ????????????????????out.write(String.valueOf(p.getAge()).getBytes());?? ????????????????????out.write((sep?+?"/t").getBytes());?? ????????????????????out.write(sep.getBytes());?? ????????????????????out.write(System.getProperty("line.separator").getBytes());?? ????????????????}?? ????????????????out.flush();?? ?????????????????? ????????????}?catch?(Exception?e)?{?? ????????????????e.printStackTrace();?? ????????????}?? ????????}?else?if?(downType.equals("Excel"))?{?? ?????????????? ?????????????? ?????????????? ?????????????? ?????????????? ????????????try?{?? ?????????????????? ????????????????Label?l?=?null;?? ????????????????WritableWorkbook?wbook?=?Workbook.createWorkbook(new?File(?? ????????????????????????"d://a.xls"));?? ?????????????????? ????????????????WritableSheet?sheet?=?wbook.createSheet("my?excel?file",?0);?? ????????????????jxl.write.WritableFont?wfc4?=?new?jxl.write.WritableFont(?? ????????????????????????WritableFont.ARIAL,?9,?WritableFont.NO_BOLD,?false,?? ????????????????????????jxl.format.UnderlineStyle.NO_UNDERLINE,?? ????????????????????????jxl.format.Colour.BLACK);?? ????????????????jxl.write.WritableCellFormat?wcfFC4?=?new?jxl.write.WritableCellFormat(?? ????????????????????????wfc4);?? ????????????????wcfFC4.setBackground(jxl.format.Colour.LIGHT_GREEN);?? ????????????????int?col?=?0;?? ????????????????sheet.setColumnView(col,?12);?? ????????????????l?=?new?Label(col,?0,?"id",?wcfFC4);?? ????????????????sheet.addCell(l);?? ????????????????col++;?? ????????????????sheet.setColumnView(col,?12);?? ????????????????l?=?new?Label(col,?0,?"name",?wcfFC4);?? ????????????????sheet.addCell(l);?? ????????????????col++;?? ????????????????sheet.setColumnView(col,?12);?? ????????????????l?=?new?Label(col,?0,?"sex",?wcfFC4);?? ????????????????sheet.addCell(l);?? ????????????????col++;?? ????????????????sheet.setColumnView(col,?12);?? ????????????????l?=?new?Label(col,?0,?"age",?wcfFC4);?? ????????????????sheet.addCell(l);?? ?? ?????????????????? ????????????????jxl.write.WritableFont?wfc5?=?new?jxl.write.WritableFont(?? ????????????????????????WritableFont.ARIAL,?9,?WritableFont.NO_BOLD,?false,?? ????????????????????????jxl.format.UnderlineStyle.NO_UNDERLINE,?? ????????????????????????jxl.format.Colour.BLACK);?? ????????????????jxl.write.WritableCellFormat?wcfFC5?=?new?jxl.write.WritableCellFormat(?? ????????????????????????wfc5);?? ????????????????for?(int?i?=?0;?i?<?list.size();?i++)?{?? ????????????????????Person?p?=?(Person)?list.get(i);?? ????????????????????int?j?=?0;?? ????????????????????l?=?new?Label(j,?i?+?1,?p.getId(),?wcfFC5);?? ????????????????????sheet.addCell(l);?? ????????????????????j++;?? ????????????????????l?=?new?Label(j,?i?+?1,?p.getName(),?wcfFC5);?? ????????????????????sheet.addCell(l);?? ????????????????????j++;?? ????????????????????l?=?new?Label(j,?i?+?1,?p.getSex(),?wcfFC5);?? ????????????????????sheet.addCell(l);?? ????????????????????j++;?? ????????????????????l?=?new?Label(j,?i?+?1,?String.valueOf(p.getAge()),?wcfFC5);?? ????????????????????sheet.addCell(l);?? ????????????????????j++;?? ????????????????}?? ?????????????????? ????????????????wbook.write();?? ????????????????wbook.close();?? ?? ????????????}?catch?(Exception?e)?{?? ????????????????e.printStackTrace();?? ????????????}?? ????????}?? ????????return?SUCCESS;?? ????}?? ????}?? ????</pre><br><br>??
操作很簡單。選擇好要導出的報表格式。點擊導出按鈕。我們在d盤相應的位置就會生成相應的報表。
OK,介紹完畢。希望可以幫助有這個需求的朋友。
------------------------------------------------------------------------------------------------------------
《Java程序員由笨鳥到菜鳥》電子版書正式發布,歡迎大家下載
http://blog.csdn.NET/csh624366188/article/details/7999247
from:?http://blog.csdn.net/csh624366188/article/details/8207600
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的Java程序员从笨鸟到菜鸟之(一百零六)java操作office和pdf文件(四)页面列表导出cvs,excel、pdf报表.的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。