web開發之匯出excel工具類

來源:互聯網
上載者:User

標籤:style   blog   java   檔案   os   2014   

web開發中,一個系統的普通需求也包括匯出excel,一般採用POI做統計報表匯出excel。

匯出excel工具類:

import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.List;import java.util.Map;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFRichTextString;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;public class ExportExcel {private ExportExcel() {super();}public static void exportExcel(List<Object> list, Map<Integer, Long> map,String[] titles) throws IOException {// 建立Excel文檔HSSFWorkbook hwb = new HSSFWorkbook();// sheet 對應一個工作頁HSSFSheet sheet = hwb.createSheet("exportReport");int colNum = titles.length;// 建立第一行HSSFRow firstrow = sheet.createRow(0);HSSFCell[] firstcell = new HSSFCell[colNum];for (int col = 0; col < colNum; col++) {firstcell[col] = firstrow.createCell(col);firstcell[col].setCellValue(new HSSFRichTextString(titles[col]));}// 插入記錄int rowNum = map.size();for (int i = 0; i < rowNum; i++) {// 從第二行開始HSSFRow row = sheet.createRow(i + 1);// 插入list中的欄位for (int col = 0; col < colNum - 2; col++) {HSSFCell cell = row.createCell(col);cell.setCellValue(list.get(col).toString());}// 插入月份或日期row.createCell(colNum - 2).setCellValue(i + 1);// 插入總量row.createCell(colNum - 1).setCellValue(map.get(i + 1));}String fileName = titles[1].substring(0, 2);if (colNum == 4) {fileName += list.get(0) + "_" + list.get(1) + "年_年度報表";} else if (colNum == 5) {fileName += list.get(0) + "_" + list.get(1) + "年" + list.get(2)+ "月_月度報表";}// 建立檔案輸出資料流,準備輸出試算表OutputStream out = new FileOutputStream("../webapps/UsedMallMinaServer/files/"+ fileName + ".xls");hwb.write(out);out.close();}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.