JavaPOI导出多个sheet

刚刚做了一个excel导出多个sheet的小功能,请大家多提建议 多多指正

 

这个是工具类

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.OutputStream;
import java.util.List;

public class NewExcelExport {
    public void exportExcel(XSSFWorkbook workbook, int sheetNum,String sheetTitle, String[] headers, List> result,
                            OutputStream out) throws Exception {
// 第一步,创建一个webbook,对应一个Excel以xsl为扩展名文件
        XSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(sheetNum, sheetTitle);
//设置列宽度大小
        sheet.setDefaultColumnWidth((short) 30);
//第二步, 生成表格第一行的样式和字体
        XSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
        style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成一个字体
        XSSFFont font = workbook.createFont();
        font.setColor(HSSFColor.BLACK.index);
//设置字体所在的行高度
        font.setFontHeightInPoints((short) 10);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
        style.setFont(font);
// 指定当单元格内容显示不下时自动换行
        style.setWrapText(true);
// 产生表格标题行
        XSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            XSSFCell cell = row.createCell((short) i);
            cell.setCellStyle(style);
            XSSFRichTextString text = new XSSFRichTextString(headers[i]);
            cell.setCellValue(text.toString());
        }
// 第三步:遍历集合数据,产生数据行,开始插入数据
        if (result != null) {
            int index = 1;
            for (List m : result) {
                row = sheet.createRow(index);
                int cellIndex = 0;
                for (String str : m) {
                    XSSFCell cell = row.createCell((int) cellIndex);
                    cell.setCellValue(str+"");
                    cellIndex++;
                }
                index++;
            }
        }
    }

}

 

这个是调用 时间仓促 后期会调整到service层调用

   @GetMapping("/download")
   @ResponseBody
   @RequiresPermissions("o:x:download")
   public void exportPbxcjhppExcel(HttpServletResponse response){
      String filePath = apiProperties.getFilePath()+Const.FILEPATHFILE+"OMPXiaoShouTemplate.xlsx"; //上传路径
      NewExcelExport excelExport = null;
      try{
         excelExport = new NewExcelExport();
         OutputStream out = new FileOutputStream(filePath);
         String[] summary = { "阿", "的","目标" ,"金额","业务线","部门","备注"};
         List> summaryData = new ArrayList>();
//       List _listSummary=new ArrayList();
         String[] water = { "客户"};
         List> waterData = Lists.newArrayList();
         List list = xiaoshouService.getCustomerPool();
         for (String s : list) {
            List rowData = new ArrayList<>();
            rowData.add(s);
            waterData.add(rowData);
         }
//       waterData.add(list);

         //3、生成格式是xlsx可存储103万行数据,如果是xls则只能存不到6万行数据
         XSSFWorkbook workbook = new XSSFWorkbook();
         //第一个表格内容
         excelExport.exportExcel(workbook, 0, "sheet1", summary, summaryData, out);
         //第二个表格内容
         excelExport.exportExcel(workbook, 1, "客户", water, waterData, out);
         //将所有的数据一起写入,然后再关闭输入流。
         workbook.write(out);
         out.close();

      }catch (Exception e){
         e.printStackTrace();
      }


   }

 

 

 

你可能感兴趣的:(Java,Java工作日记)