美文网首页
java随笔(十三)生成excle文件

java随笔(十三)生成excle文件

作者: 那谁319 | 来源:发表于2018-06-24 22:31 被阅读0次

    一种动态生成excle表格的方法,maven依赖包

    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi</artifactId>
          <version>3.15</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi-ooxml</artifactId>
          <version>3.15</version>
        </dependency>
    
    import java.util.List;
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.usermodel.HSSFFont;
    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;
    import org.apache.poi.hssf.util.HSSFColor;
    
    /**
     * @author haishen
     * @date 2018/6/24
     */
    public class ExcelUtils {
    
        /**
         * @param workbook
         * @param sheetNum   (sheet的位置,0表示第一个表格中的第一个sheet)
         * @param sheetTitle (sheet的名称)
         * @param headers    (表格的标题)
         * @param result     (表格的数据)
         * @throws Exception
         * @Title: exportExcel
         * @Description: 导出Excel的方法
         */
        public void exportExcel(HSSFWorkbook workbook, int sheetNum,
                                String sheetTitle, String[] headers, List<List<Object>> result) throws Exception {
            // 生成一个表格
            HSSFSheet sheet = workbook.createSheet();
            workbook.setSheetName(sheetNum, sheetTitle);
            // 设置表格默认列宽度为20个字节
            sheet.setDefaultColumnWidth((short) 20);
            // 生成一个样式
            HSSFCellStyle style = workbook.createCellStyle();
            // 设置这些样式
            style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
            // 生成一个字体
            HSSFFont font = workbook.createFont();
            font.setColor(HSSFColor.BLACK.index);
            font.setFontHeightInPoints((short) 12);
            // 把字体应用到当前的样式
            style.setFont(font);
            // 指定当单元格内容显示不下时自动换行
            style.setWrapText(true);
            // 产生表格标题行
            HSSFRow row = sheet.createRow(0);
            for (int i = 0; i < headers.length; i++) {
                HSSFCell cell = row.createCell((short) i);
                cell.setCellStyle(style);
                HSSFRichTextString text = new HSSFRichTextString(headers[i]);
                cell.setCellValue(text.toString());
            }
            // 遍历集合数据,产生数据行
            if (result != null) {
                int index = 1;
                for (List<Object> m : result) {
                    row = sheet.createRow(index);
                    int cellIndex = 0;
                    for (Object str : m) {
                        HSSFCell cell = row.createCell((short) cellIndex);
                        cell.setCellValue(str.toString());
                        cellIndex++;
                    }
                    index++;
                }
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:java随笔(十三)生成excle文件

          本文链接:https://www.haomeiwen.com/subject/ylasyftx.html