經過兩天的研究,現在對excel導出有點心得了。我們使用的excel導出的jar包是poi這個阿帕奇公司的一個項目,後來被擴充了。是比較好用的excel導出工具。
下面來認識一下這個它吧。
我們知道要創建一張excel你得知道excel由什麼組成,比如說sheet也就是一個工作表格,例如一行,一個單元格,單元格格式,單元格內容格式…這些都對應着poi裏面的一個類。
一個excel表格:
HSSFWorkbook wb = new HSSFWorkbook();
一個工作表格(sheet):
HSSFSheet sheet = wb.createSheet("測試表格");
一行(row):
HSSFRow row1 =
sheet.createRow(0);
一個單元格(cell):
HSSFCell cell2 = row2.createCell((short)0);
單元格格式(cellstyle):
HSSFCellStyle style4 = wb.createCellStyle()
單元格內容格式()
HSSFDataFormat format= wb.createDataFormat();
知道上面的基本知識後下面學起來就輕鬆了。我直接貼代碼,這段代碼會產生一個表格其實,代碼長,但是很簡單,一看就明白
importins.framework.dao.GenericDaoHibernate; importjava.io.FileOutputStream; importjava.io.IOException; importjava.sql.Connection; importjava.sql.ResultSet; import java.sql.SQLException; importjava.sql.Statement; importjava.util.ArrayList; importjava.util.Date; importjava.util.List; importjavax.annotation.Resource; importorg.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; importorg.apache.poi.hssf.usermodel.HSSFDataFormat; importorg.apache.poi.hssf.usermodel.HSSFRow; importorg.apache.poi.hssf.usermodel.HSSFSheet; importorg.apache.poi.hssf.usermodel.HSSFWorkbook; importorg.apache.poi.hssf.util.Region; importorg.apache.poi.ss.usermodel.Font; importorg.hibernate.HibernateException; importorg.hibernate.SQLQuery; importorg.hibernate.Session; importorg.springframework.orm.hibernate3.HibernateCallback; importorg.springframework.orm.hibernate3.HibernateTemplate; importorg.springframework.stereotype.Component; importcom.reportforms.service.facade.FundDayDetailService; @Component("fundDayDetailService") publicclass FundDayDetailsServiceSpringImpl implements FundDayDetailService { @Resource private HibernateTemplatehibernateTemplate; public HibernateTemplategetHibernateTemplate() { return hibernateTemplate; } public voidsetHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate =hibernateTemplate; } @SuppressWarnings("deprecation") public void outExcel(String fundsType,Date tradeDate, String assetsTypeCode){ HSSFWorkbook wb = newHSSFWorkbook(); //--->創建了一個excel文件 HSSFSheet sheet =wb.createSheet("理財資金報表"); //--->創建了一個工作簿 HSSFDataFormat format=wb.createDataFormat(); //--->單元格內容格式 sheet.setColumnWidth((short)3, 20*256); //---》設置單元格寬度,因爲一個單元格寬度定了那麼下面多有的單元格高度都確定了所以這個方法是sheet的 sheet.setColumnWidth((short)4, 20*256); //--->第一個參數是指哪個單元格,第二個參數是單元格的寬度 sheet.setDefaultRowHeight((short)300); // ---->有得時候你想設置統一單元格的高度,就用這個方法 //樣式1 HSSFCellStyle style =wb.createCellStyle(); // 樣式對象 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直 style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平 //設置標題字體格式 Font font = wb.createFont(); //設置字體樣式 font.setFontHeightInPoints((short)20); //--->設置字體大小 font.setFontName("CourierNew"); //---》設置字體,是什麼類型例如:宋體 font1.setItalic(true); //--->設置是否是加粗 style1.setFont(font1); //--->將字體格式加入到style1中 //style1.setFillForegroundColor(IndexedColors.DARK_YELLOW.getIndex()); //style1.setFillPattern(CellStyle.SOLID_FOREGROUND);設置單元格顏色 style1.setWrapText(true); //設置是否能夠換行,能夠換行爲true style1.setBorderBottom((short)1); //設置下劃線,參數是黑線的寬度 style1.setBorderLeft((short)1); //設置左邊框 style1.setBorderRight((short)1); //設置有邊框 style1.setBorderTop((short)1); //設置下邊框 style4.setDataFormat(format.getFormat("¥#,##0")); //--->設置爲單元格內容爲貨幣格式 style5.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%")); //--->設置單元格內容爲百分數格式 //表格第一行 HSSFRow row1 =sheet.createRow(0); //--->創建一行 // 四個參數分別是:起始行,起始列,結束行,結束列 sheet.addMergedRegion(newRegion(0, (short) 0, 0, (short) 15)); row1.setHeightInPoints(25); HSSFCell cell1 =row1.createCell((short)0); //--->創建一個單元格 cell1.setCellStyle(style); cell1.setCellValue("總公司資金運用日報明細表(理財資金)"); //表格第二行 sheet.addMergedRegion(newRegion(1,(short)0,1,(short)15)); HSSFRow row2 = sheet.createRow(1); HSSFCell cell2 =row2.createCell((short)0); cell2.setCellValue("報告日期:"+new Date()); cell2.setCellStyle(style2); //表格第三行 sheet.addMergedRegion(newRegion(2,(short)0,2,(short)15)); HSSFRow row3 = sheet.createRow(2); HSSFCellcell3 = row3.createCell((short)0); cell3.setCellValue("交易日期:"+new Date()); cell3.setCellStyle(style2); //表格第四行 sheet.addMergedRegion(newRegion(3, (short)0, 3, (short)2)); HSSFRow row4 = sheet.createRow(3); row4.setHeightInPoints((short)75); HSSFCell cell4 =row4.createCell((short)0); HSSFCell cell4_0_1 =row4.createCell((short)1); cell4_0_1.setCellStyle(style2); HSSFCell cell4_0_2 =row4.createCell((short)2); cell4_0_2.setCellStyle(style2); cell4.setCellStyle(style1); cell4.setCellValue("代碼/品種"); HSSFCell cell4_1 =row4.createCell((short)3); cell4_1.setCellStyle(style1); cell4_1.setCellValue("投資類型"); HSSFCell cell4_2 =row4.createCell((short)4); cell4_2.setCellStyle(style1); cell4_2.setCellValue("證券賬戶"); HSSFCell cell4_3 =row4.createCell((short)5); cell4_3.setCellStyle(style1); cell4_3.setCellValue("份額\n單位:元"); HSSFCell cell4_4 =row4.createCell((short)6); cell4_4.setCellStyle(style1); cell4_4.setCellValue("結轉總成本\n單位:元"); HSSFCell cell4_5 =row4.createCell((short)7); cell4_5.setCellStyle(style1); cell4_5.setCellValue("總市值\n單位:元"); HSSFCell cell4_6 =row4.createCell((short)8); cell4_6.setCellStyle(style1); cell4_6.setCellValue("結轉成本價\n單位:元"); HSSFCell cell4_7 =row4.createCell((short)9); cell4_7.setCellStyle(style1); cell4_7.setCellValue("市價\n單位:元"); HSSFCell cell4_8 =row4.createCell((short)10); cell4_8.setCellStyle(style1); cell4_8.setCellValue("持有期收益\n單位:%"); HSSFCell cell4_9 =row4.createCell((short)11); cell4_9.setCellStyle(style1); cell4_9.setCellValue("總收益率(總收益/結轉總成本)\n單位:%"); HSSFCell cell4_10 =row4.createCell((short)12); cell4_10.setCellStyle(style1); cell4_10.setCellValue("前一日漲跌幅\n單位:%"); HSSFCell cell4_11 =row4.createCell((short)13); cell4_11.setCellStyle(style1); cell4_11.setCellValue("盈虧\n單位:元"); HSSFCell cell4_12 =row4.createCell((short)14); cell4_12.setCellStyle(style1); cell4_12.setCellValue("以實現收益\n單位:元"); HSSFCell cell4_13 =row4.createCell((short)15); cell4_13.setCellStyle(style1); cell4_13.setCellValue("浮盈(虧)+已實現收益\n單位:元"); //第五行 sheet.addMergedRegion(newRegion(4, (short)0, 4, (short)2)); HSSFRow row5 = sheet.createRow(4); HSSFCell cell5 =row5.createCell((short)0); HSSFCell cell5_1 =row5.createCell((short)1); cell5_1.setCellStyle(style2); HSSFCell cell5_2 =row5.createCell((short)2); cell5_2.setCellStyle(style2); cell5.setCellValue("投資資產合計"); cell5.setCellStyle(style2); //第六行 sheet.addMergedRegion(newRegion(5, (short)0, 5, (short)2)); HSSFRow row6 = sheet.createRow(5); HSSFCell cell6 =row6.createCell((short)0); HSSFCell cell6_1 =row6.createCell((short)1); cell6_1.setCellStyle(style2); HSSFCell cell6_2 = row6.createCell((short)2); cell6_2.setCellStyle(style2); cell6.setCellValue("2、股票"); cell6.setCellStyle(style2); //第七行 sheet.addMergedRegion(newRegion(6, (short)0, 6, (short)2)); HSSFRow row7 = sheet.createRow(6); HSSFCell cell7 = row7.createCell((short)0); HSSFCell cell7_1 =row7.createCell((short)1); cell7_1.setCellStyle(style2); HSSFCell cell7_2 =row7.createCell((short)2); cell7_2.setCellStyle(style2); cell7.setCellValue("2.1、境內A股"); cell7.setCellStyle(style2); //第八行 sheet.addMergedRegion(newRegion(7, (short)0, 7, (short)2)); HSSFRow row8 = sheet.createRow(7); HSSFCell cell8 =row8.createCell((short)0); HSSFCell cell8_1 =row8.createCell((short)1); cell8_1.setCellStyle(style2); HSSFCell cell8_2 = row8.createCell((short)2); cell8_2.setCellStyle(style2); cell8.setCellValue("非限售股"); cell8.setCellStyle(style2); Connection conn = null; Statement sm = null; ResultSet rs = null; try{ conn =hibernateTemplate.getSessionFactory().openSession().connection(); sm =conn.createStatement(); rs = sm.executeQuery(sql); int j = 0; //增加行 while(rs.next()){ HSSFRow rowN =sheet.createRow(8+j); //第9行...第n行 List<String>list = new ArrayList<String>(); //存放每一行數據 for(int i = 1 ; i<= 16 ; i++ ){ list.add(rs.getString(i)); } for(int k = 0 ; k< 16 ; k++){ if(k<5){ HSSFCellcellN = rowN.createCell((short)k); cellN.setCellStyle(style3); cellN.setCellValue(list.get(k)); } if((k>=5&& k<=9)||(k>=13)){ HSSFCellcellN = rowN.createCell((short)k); cellN.setCellStyle(style4); cellN.setCellValue(Double.parseDouble(list.get(k))); } if(k>=10&& k<= 12){ HSSFCellcellN = rowN.createCell((short)k); cellN.setCellStyle(style5); cellN.setCellValue(Double.parseDouble(list.get(k))); } } j++; } }catch(Exception e){ e.printStackTrace(); }finally{ if(rs != null){ try { rs.close(); } catch (SQLExceptione) { // TODOAuto-generated catch block e.printStackTrace(); } } if(sm != null){ try { sm.close(); } catch(SQLException e) { // TODOAuto-generated catch block e.printStackTrace(); } } if(conn != null){ try { conn.close(); } catch(SQLException e) { // TODOAuto-generated catch block e.printStackTrace(); } } } FileOutputStream fileOut = null; try{ fileOut = new FileOutputStream("d:\\workbook.xls"); wb.write(fileOut); //fileOut.close(); System.out.print("OK"); }catch(Exception e){ e.printStackTrace(); } finally{ if(fileOut != null){ try { fileOut.close(); } catch (IOExceptione) { // TODOAuto-generated catch block e.printStackTrace(); } } } } }
特別注意!!!!本代碼不能直接運行,主要是讓你知道各種方法的用處。附件是一個能夠直接運行出的表格。請注意要有數據庫支持,這是一個web項目,但是java文件可以直接運行。裏面包含jar包