java根據(jù)excel 模板導(dǎo)出excel

//這個(gè)用模板導(dǎo)出,省略了創(chuàng)建表頭的方法,可以把模板里面的全部復(fù)制下來(lái),放到要導(dǎo)出的excel里面

//首先要記住,excel 第一行是從0開始的,列也是如此

//訪問方法

public String na(HttpServletResponse response,HttpServletRequest request){

????//查詢要到處的數(shù)據(jù)

List<Map<string,string>> dataSourceList= null;

try {

????????ExcelDownloadUtil.ExcelByModel("測(cè)試模板導(dǎo)出", modelURLString, dataSourceList, response, sheetNameStrings, keysStrings, 6);

} catch (Exception e) {

????????e.printStackTrace();

}

????return "SUCESS";

}



在模板中添加數(shù)據(jù)

public static void ExcelByModel(String ExcelName, String ModelURl, List<Map<String,String>> dataSource,HttpServletResponse response, String[] sheetNames, String[] keyNames, int rowNum) throws Exception {

// 設(shè)置導(dǎo)出Excel報(bào)表的導(dǎo)出形式

response.setContentType("application/vnd.ms-excel");

// 設(shè)置導(dǎo)出Excel報(bào)表的響應(yīng)文件名

String fileName = new String(ExcelName.getBytes("utf-8"), "ISO-8859-1");

response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");//導(dǎo)出的文件名稱

// 創(chuàng)建一個(gè)輸出流

OutputStream fileOut = response.getOutputStream();

// 讀取模板文件路徑

File file = new File(ModelURl);

FileInputStream fins = new FileInputStream(file);

POIFSFileSystem fs = new POIFSFileSystem(fins);

// 讀取Excel模板

HSSFWorkbook wb = new HSSFWorkbook(fs);

HSSFSheet sheet = wb.getSheetAt(0);//獲取第一頁(yè)sheet頁(yè)

sheet.autoSizeColumn(1);

HSSFRow rowCellStyle1 = sheet.getRow(2);//sheet頁(yè)的第二行

HSSFCellStyle columnOne01 = rowCellStyle1.getCell(0).getCellStyle();//獲取sheet頁(yè)第二行的樣式

// 設(shè)置邊框樣式(樣式自己選擇)

//? ? ? ? HSSFCellStyle style = wb.createCellStyle();

//? ? ? ? style.setBorderBottom(HSSFCellStyle.BORDER_THIN);

//? ? ? ? style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

//? ? ? ? style.setBorderRight(HSSFCellStyle.BORDER_THIN);

//? ? ? ? style.setBorderTop (HSSFCellStyle.BORDER_THIN);

// 設(shè)置邊框樣式的顏色

//? ? ? ? style.setBottomBorderColor(HSSFColor.BLACK.index);

//? ? ? ? style.setLeftBorderColor(HSSFColor.BLACK.index);

//? ? ? ? style.setRightBorderColor(HSSFColor.BLACK.index);

//? ? ? ? style.setTopBorderColor(HSSFColor.BLACK.index);


for (int j = 0; j <20; j++) {

HSSFRow row = sheet.getRow(j+3);// 創(chuàng)建第二行

HSSFCell cellHeard1 = row.getCell(1); ? ?//獲取模板的第2個(gè)單元格b

HSSFCell cellHeard2 = row.getCell(2);

HSSFCell cellHeard3 = row.getCell(3);

HSSFCell cellHeard4 = row.getCell(4);

HSSFCell cellHeard5 = row.getCell(5);

HSSFCell cellHeard6 = row.getCell(6);

HSSFCell cellHeard7 = row.getCell(7);

// 在該單元格內(nèi)輸入內(nèi)容

cellHeard1.setCellValue(j+1); //序號(hào)

cellHeard1.setCellStyle(columnOne01);//獲取模板單元格樣式

//單元格添加數(shù)據(jù)

cellHeard2.setCellValue("1");

cellHeard2.setCellStyle(columnOne01);

//單元格添加數(shù)據(jù)

cellHeard3.setCellValue("2");

cellHeard3.setCellStyle(columnOne01);

//單元格添加數(shù)據(jù)

cellHeard4.setCellValue(3);

cellHeard4.setCellStyle(columnOne01);

//單元格添加數(shù)據(jù)

cellHeard5.setCellValue(4);

cellHeard5.setCellStyle(columnOne01);

//單元格添加數(shù)據(jù)

cellHeard6.setCellValue(5);

cellHeard6.setCellStyle(columnOne01);

//單元格添加數(shù)據(jù)

cellHeard7.setCellValue(6);

cellHeard7.setCellStyle(columnOne01);

}

// 寫入流

wb.write(fileOut);

// 關(guān)閉流

fileOut.close();

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 項(xiàng)目中使用POI生成excle報(bào)表 maven: <groupId>org.apache.poi poi <ve...
    大大大浣熊閱讀 4,784評(píng)論 0 6
  • Apache POI 是用Java編寫的免費(fèi)開源的跨平臺(tái)的 Java API,Apache POI提供API給Ja...
    玩味Orz閱讀 2,759評(píng)論 0 0
  • 轉(zhuǎn)自鏈接 3.項(xiàng)目實(shí)踐 3.1基于.xls模板生成Excel文件 3.2生成九九乘法表 3.3生成一張工資單 3....
    腿毛褲閱讀 3,772評(píng)論 0 0
  • 使用首先需要了解他的工作原理 1.POI結(jié)構(gòu)與常用類 (1)創(chuàng)建Workbook和Sheet (2)創(chuàng)建單元格 (...
    長(zhǎng)城ol閱讀 8,745評(píng)論 2 25
  • excel文件導(dǎo)出有兩種,一種是以io流直接生成,一種是在本地生成一個(gè)excel文件(fileoutputStre...
    Easy的幸福閱讀 1,102評(píng)論 2 2

友情鏈接更多精彩內(nèi)容