java原生Excel单元格合并自定义导出
生活随笔
收集整理的這篇文章主要介紹了
java原生Excel单元格合并自定义导出
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.簡單案例
//初始化HSSFWorkbook workbook = new HSSFWorkbook();HSSFCellStyle style = workbook.createCellStyle();style.setAlignment(HorizontalAlignment.CENTER);style.setVerticalAlignment(VerticalAlignment.CENTER);//創建sheetHSSFSheet sheet = workbook.createSheet("sheet");/** 創建第0行 **/HSSFRow row0 = sheet.createRow(0);//第一行抬頭第1列HSSFCell cell_00 = row0.createCell(0);cell_00.setCellStyle(style);cell_00.setCellValue("日期");//第一行抬頭第2列HSSFCell cell_01 = row0.createCell(1);cell_01.setCellStyle(style);cell_01.setCellValue("午別");//第一行抬頭第3列HSSFCell cc = row0.createCell(2);cc.setCellStyle(style);cc.setCellValue("hhh");/** 創建第1行 **/HSSFRow row1 = sheet.createRow(1);//第二行抬頭第1列HSSFCell cell_10 = row1.createCell(0);cell_10.setCellStyle(style);cell_10.setCellValue("20180412");//第二行抬頭第二列HSSFCell cell_11 = row1.createCell(1);cell_11.setCellStyle(style);cell_11.setCellValue("上午");//第二行抬頭第三列HSSFCell cell_2 = row1.createCell(2);cell_2.setCellStyle(style);cell_2.setCellValue("上午");/** 創建第3行 **/HSSFRow row2 = sheet.createRow(2);//第三行抬頭第二列HSSFCell cell_21 = row2.createCell(1);cell_21.setCellStyle(style);cell_21.setCellValue("下午");/** 創建第4行 **/HSSFRow row3 = sheet.createRow(3);//第四行抬頭第二列HSSFCell cell_23 = row3.createCell(1);cell_23.setCellStyle(style);cell_23.setCellValue("測試哦");// 合并日期占兩行(4個參數,分別為起始行,結束行,起始列,結束列)// 行和列都是從0開始計數,且起始結束都會合并// 這里是合并excel中日期的兩行為一行 合并第一行-第三行CellRangeAddress region = new CellRangeAddress(1, 3, 0, 0);sheet.addMergedRegion(region);File file = new File("E:\\demo.xls");FileOutputStream fout = null;try {fout = new FileOutputStream(file);} catch (FileNotFoundException e) {e.printStackTrace();}try {workbook.write(fout);} catch (IOException e) {e.printStackTrace();}finally {try {fout.close();} catch (IOException e) {e.printStackTrace();}}1.1 導出文件路徑用戶自定義
?
// 獲取輸出流(用于文件最后導出自定位置使用)OutputStream os = response.getOutputStream();response.setContentType("application/vnd.ms-excel");response.setHeader("Content-disposition", "attachment; filename=" + System.currentTimeMillis()+".xls");response.setHeader("filename",System.currentTimeMillis()+".xls");response.setHeader("Pragma", "No-cache");response.setHeader("Cache-Control", "No-cache");response.setDateHeader("Expires", 0);workbook.write(os);os.close();
總結
以上是生活随笔為你收集整理的java原生Excel单元格合并自定义导出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Apple]通过Apple ID登录A
- 下一篇: 2-1-单链表顺序存储结构-线性表-第2