java 生成Excel

首先網上關於代碼創建excel大致分爲兩種:
1.採用poi;
2.採用jxl;
(菜鳥個人觀點,當然也有很多現成的工具類等等)

我所採用的是poi的方式:
首先創建Workbook(poi提供了兩種):HSSFWorkbook、XSSFWorkbook
注:HSSFWorkbook 是後綴爲xls(2003-2007)版本
XSSFWorkbook 爲2007以上版本

HSSFWorkbook  xls = new  HSSFWorkbook();//創建Workbook
HSSFSheet sheet = xls.createSheet("newsheet");//創建sheet
HSSFCellStyle style = xls.createCellStyle();//設置單元格格式  
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 創建一個居中格式  
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中

接下來創建sheet頁內容
1、Title

HSSFRow row = sheet.createRow((int) 0);  
//合併單元格   
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));
HSSFCell cell = row.createCell(0);
cell.setCellValue("區域");  
cell.setCellStyle(style);  
cell = row.createCell(2);  
cell.setCellValue("商品名稱");  
cell.setCellStyle(style);  
cell = row.createCell(3); 
cell.setCellValue("當日兌換量");  
cell.setCellStyle(style);  
cell = row.createCell(4);  
cell.setCellValue("成本價");  
cell.setCellStyle(style);  
cell = row.createCell(5);  
cell.setCellValue("結算單價");  
cell.setCellStyle(style);  
cell = row.createCell(6);  
cell.setCellValue("銷售價");
cell.setCellStyle(style); 
cell = row.createCell(7);  
cell.setCellValue("當日兌換金額");
cell.setCellStyle(style);  
cell = row.createCell(8);  
cell.setCellValue("當日收入");
cell.setCellStyle(style);  
cell = row.createCell(9);  
cell.setCellValue("累計兌換量");
cell.setCellStyle(style);  
cell = row.createCell(10);  
cell.setCellValue("累計收入");
cell.setCellStyle(style);  
cell = row.createCell(11);  
cell.setCellValue("累計兌換金額");

注:sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));
合併單元格
這裏寫圖片描述
(注意包的路徑:org.apache.poi.ss.util.CellRangeAddress,有的爲org.apache.poi.hssf.util.CellRangeAddress已廢棄)
這裏寫圖片描述

方法的參數(index座標從0開始)
firstRow :起始行;
lastRow:結束行;
firstCol:起始單元格;
lastCol:結束單元格;

2、單元格內容

HSSFRow row = sheet.createRow(1);
sheet.addMergedRegion(new CellRangeAddress(1, 3, 0, 0));
HSSFCell cell = row.createCell(0);
cell.setCellValue("江蘇");  
cell.setCellStyle(style);
cell = row.createCell(1);
sheet.addMergedRegion(new CellRangeAddress(1, 2, 1, 1));
cell.setCellValue("移動");  
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("aaa");  
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("8");  
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("20.00");  
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("32.98");  
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue("40.00");  
cell.setCellStyle(style);
cell = row.createCell(7);
cell.setCellValue("320.00");  
cell.setCellStyle(style);
cell = row.createCell(8);
cell.setCellValue("263.84");  
cell.setCellStyle(style);
cell = row.createCell(9);
cell.setCellValue("1110");  
cell.setCellStyle(style);
cell = row.createCell(10);
cell.setCellValue("36607.80");  
cell.setCellStyle(style);
cell = row.createCell(11);
cell.setCellValue("44400.00");  
cell.setCellStyle(style);
row = sheet.createRow(2);
cell = row.createCell(2);
cell.setCellValue("aaa");  
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("8");  
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("20.00");  
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("32.98");  
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue("40.00");  
cell.setCellStyle(style);
cell = row.createCell(7);
cell.setCellValue("320.00");  
cell.setCellStyle(style);
cell = row.createCell(8);
cell.setCellValue("263.84");  
cell.setCellStyle(style);
cell = row.createCell(9);
cell.setCellValue("1110");  
cell.setCellStyle(style);
cell = row.createCell(10);
cell.setCellValue("36607.80");  
cell.setCellStyle(style);
cell = row.createCell(11);
cell.setCellValue("44400.00");  
cell.setCellStyle(style);
row = sheet.createRow(3);
sheet.addMergedRegion(new CellRangeAddress(3, 3, 1, 1));
cell = row.createCell(1);
cell.setCellValue("電信");  
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("aaa");  
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("8");  
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("20.00");  
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("32.98");  
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue("40.00");  
cell.setCellStyle(style);
cell = row.createCell(7);
cell.setCellValue("320.00");  
cell.setCellStyle(style);
cell = row.createCell(8);
cell.setCellValue("263.84");  
cell.setCellStyle(style);
cell = row.createCell(9);
cell.setCellValue("1110");  
cell.setCellStyle(style);
cell = row.createCell(10);
cell.setCellValue("36607.80");  
cell.setCellStyle(style);
cell = row.createCell(11);
cell.setCellValue("44400.00");  
cell.setCellStyle(style);
row = sheet.createRow(4);
sheet.addMergedRegion(new CellRangeAddress(4, rowIndex, 0, 2));
cell = row.createCell(0);
cell.setCellValue("江蘇合計");  
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("2");  
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("-");  
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("-");  
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue("-");  
cell.setCellStyle(style);
cell = row.createCell(7);
cell.setCellValue("320");  
cell.setCellStyle(style);
cell = row.createCell(8);
cell.setCellValue("263.84");  
cell.setCellStyle(style);
cell = row.createCell(9);
cell.setCellValue("111");  
cell.setCellStyle(style);
cell = row.createCell(10);
cell.setCellValue("3688");  
cell.setCellStyle(style);
cell = row.createCell(11);
cell.setCellValue("4200");  
cell.setCellStyle(style);

這裏寫圖片描述

到這裏 一個excel就已用java創建完畢

接着需要把生成的Excel輸出

String webParentPath = new File(request.getSession().getServletContext().getRealPath("/")).getParent();// 當前WEB環境的上層目錄
String realPath = webParentPath + "/file/";// 文件上傳到服務器的真實路徑
File f = new File(realPath);
try{
if (!f.isDirectory()) {
          f.mkdirs();
      }

FileOutputStream xlsxfileOut = new FileOutputStream(realPath+"123.xls");
xls.write(xlsxfileOut);
xlsxfileOut.close();

以上爲demo關鍵代碼;小菜獻上(注代碼邏輯還需重新排列)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章