精华内容
下载资源
问答
  • java创建excel文件
    千次阅读
    2012-10-23 14:27:04

    Excel现在运用的很广泛,有的时候需要被做成数据库,这里介绍一种java创建读取Excel的方法。

    PIO是Apache Jakarta项目下的一个开源工具包,对Excel操作封装的很好。

    PIO包可以再我的资源里的下载!

    创建Excel文件的步骤如下:

    1. 首先创建一个文件输入流
    2. 给予文件输出流创建Excel工作薄
    3. 在工作薄中创建工作表,并为其命名
    4. 在工作表中循环创立新行
    5. 每行创建列
    6. 向单元格中写入数据,注意设置单元格字符集为”UTF_16“,否则写入的中文阿精出现乱码。

    具体代码如下:

    package com.zsmj.utilit;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    
    import org.apache.poi.hssf.usermodel.*;
    /**
     * 利用POI工具创建Excel工作薄和工作表,并向其中写入内容
     * @author la
     *
     */
    public class CreateExcel {
    	
    	private void createExcel()throws IOException {
    		String excelFile="myexcel.xls";
    		FileOutputStream fos=new FileOutputStream(excelFile);
    		HSSFWorkbook wb=new HSSFWorkbook();//创建工作薄
    		HSSFSheet sheet=wb.createSheet();//创建工作表
    		wb.setSheetName(0, "sheet0");//设置工作表名
    		
    		HSSFRow row=null;
    		HSSFCell cell=null;
    		for (int i = 0; i < 10; i++) {
    			row=sheet.createRow(i);//新增一行
    			cell=row.createCell((short)0);//新增一列
    			cell.setEncoding(HSSFCell.ENCODING_UTF_16);//设置单元格的字符集
    			cell.setCellType(i);//向单元格中写入数据
    			cell=row.createCell((short)0);
    			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
    			cell.setCellValue("第"+i+"行");
    		}
    		wb.write(fos);
    		fos.close();
    	}
    	/**
    	 * @param args
    	 *2012-10-23
    	 *void
    	 * @throws IOException 
    	 */
    	public static void main(String[] args) throws IOException {
    		new CreateExcel().createExcel();
    
    	}
    
    }
    


    更多相关内容
  • Java创建Excel文件

    千次阅读 2022-03-12 19:48:00
    Java创建Excel文件
     public synchronized static int writeExcel(JSONArray jsonArray,int k) {
            //创建Excel文件薄
            HSSFWorkbook workbook=new HSSFWorkbook();
            //创建工作表sheeet
            HSSFSheet sheet=workbook.createSheet();
            //创建第一行
            HSSFRow row=sheet.createRow(0);
            String[] title={"货号","中文名","英文名","CAS","分子式","分子量","图片网址","svg网址"};
            HSSFCell cell_title = null;
            for (int i=0;i<title.length;i++){
                cell_title=row.createCell(i);
                cell_title.setCellValue(title[i]);
            }
            //追加数据
            for (int i=0;i<jsonArray.size();i++){
                JSONObject jsonObject =(JSONObject) jsonArray.get(i);
                HSSFRow nextrow=sheet.createRow(i+1);
                HSSFCell cell_value=nextrow.createCell(0);
                cell_value.setCellValue("");
                cell_value=nextrow.createCell(1);
                cell_value.setCellValue(TransApi.EN_to_ZH(jsonObject.get("name").toString()));
                cell_value=nextrow.createCell(2);
                cell_value.setCellValue(jsonObject.get("name").toString());
                cell_value=nextrow.createCell(3);
                cell_value.setCellValue(jsonObject.get("CAS").toString());
                cell_value=nextrow.createCell(4);
                cell_value.setCellValue(jsonObject.get("FCS").toString());
                cell_value=nextrow.createCell(5);
                cell_value.setCellValue(jsonObject.get("MW").toString());
                cell_value=nextrow.createCell(6);
                cell_value.setCellValue(jsonObject.get("img").toString());
                cell_value=nextrow.createCell(7);
                cell_value.setCellValue(jsonObject.get("svg").toString());
            }
            //创建一个文件
            try {
                File file=new File("D:/pacong/poi_test_"+k+".xls");
                file.createNewFile();
                FileOutputStream stream= FileUtils.openOutputStream(file);
                workbook.write(stream);
                stream.close();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                return 1;
            }
        }

    展开全文
  • javaEXCEL中写入数据。里面有一个ExcelWrite.java文件和一个jxl的jar包。
  • Java写出Excel的.jar包,包含jxl.jar、dom4j.jar、poi、xmlbeans等多个包
  • Java创建Excel文件所需的所有依赖包:xmlbeans-2.3.0.jar,poi-ooxml-schemas-3.9-20121203.jar,poi-ooxml-3.9-20121203.jar,poi-3.9-20121203.jar,jxl.jar,dom4j.jar
  • java创建excel文件所需的jar包(jxl.jar , poi.jar)
  • //创建Excel文件薄 HSSFWorkbook workbook=new HSSFWorkbook(); //创建工作表sheeet HSSFSheet sheet=workbook.createSheet(); HSSFCell cell_title = null; //追加数据 for (int i=0;i map =list.get(i); HSSFRow ...
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    
    
    public String implementReturnMessage(Key[] arg0, String arg1, UFDate arg2)
    			throws BusinessException {
    		//视图名称
    		Object view = arg0[0].getValue();
    		//部门名称
    		Object dept = arg0[1].getValue();
    		//公司主键
    		Object corp = "1001";
    		String sql = "select * from "+view+" where pk_dept = "+"'"+dept+"' and pk_corp=(SELECT unitname FROM bd_corp WHERE pk_corp = '"+corp+"')";  
    		List<Map<String, Object> > list =  (List<Map<String, Object> >) getDAO().executeQuery(sql, new MapListProcessor());
    		//查询表所有列
    		List<Object[]> result = (List<Object[]>) dao.executeQuery("SELECT column_name FROM user_tab_cols where table_name = upper('"+view+"')", new ArrayListProcessor());
            //创建Excel文件薄
            HSSFWorkbook workbook=new HSSFWorkbook();
            //创建工作表sheeet
            HSSFSheet sheet=workbook.createSheet();        
            HSSFCell cell_title = null;
            //追加数据
            for (int i=0;i<list.size();i++){
            	Map<String,Object> map =list.get(i);
                HSSFRow rows=sheet.createRow(i);
                //列数
                int row = 0;
                for (Object[] obj : result){
                	//列递增
                    cell_title=rows.createCell(row++);
                    //自适应列宽
                    setSizeColumn(sheet, row);
                    //第一行添加标题
                    if(i==0){
                    cell_title.setCellValue(obj[0].toString());
                    i=0;
                    }else{
                    	//列赋值 
                    	if(map.get(obj[0])==null){
                            cell_title.setCellValue("");
                    	}else{
                            cell_title.setCellValue(map.get(obj[0].toString()).toString());
                    	}
                    }
                }	
            }
            //创建一个文件
            try {
                FileOutputStream fout = new FileOutputStream("E:/办公费明细.xls");
                workbook.write(fout);
                fout.close();
            }catch (Exception e){
                e.printStackTrace();
            }
                return null;
        }
    
    
      /**
        * 自适应列宽
        * @param sheet 表
        * @param columnLength 列
        */
    private static void setSizeColumn(HSSFSheet sheet, int columnLength) {
             for (int columnNum = 0; columnNum <= columnLength; columnNum++) {
                 int columnWidth = sheet.getColumnWidth(columnNum) / 256;
                 for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
                     HSSFRow currentRow; // 当前行未被使用过
                     if (sheet.getRow(rowNum) == null) {
                         currentRow = sheet.createRow(rowNum);
                     } else {
                         currentRow = sheet.getRow(rowNum);
                     }
                     if (currentRow.getCell(columnNum) != null) {
                         HSSFCell currentCell = currentRow.getCell(columnNum);
                         if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                             int length = currentCell.getStringCellValue().getBytes().length;
                             if (columnWidth < length) {
                                 columnWidth = length;
                             }
                         }
                     }
                 }
                 sheet.setColumnWidth(columnNum, columnWidth * 256);
             }
         }

    展开全文
  • Java创建Excel(汇总)

    千次阅读 2021-12-30 15:18:53
    本文通过向表格中插入用户名称、照片等数据,说明如何使用easyExcel生成excel文件 https://www.yuque.com/easyexcel/doc/easyexcel 导入依赖 <dependency> <groupId>com.alibaba</groupId> ...

    通过easyExcel将数据动态写入excel表格中。并灵活设置单元格、表头等内容。

    本文通过向表格中插入用户名称、照片等数据,说明如何使用easyExcel生成excel文件

    easyExcel官方文档

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>easyexcel</artifactId>
                <version>2.2.4</version>
            </dependency>

    默认格式

    实体类

    public class UserExcel {
        @ExcelProperty(value = {"C808","序号"},index = 0)
        private Integer id;
        @ExcelProperty(value = {"C808","姓名"},index = 1)
        private String name;
        @ExcelProperty(value = {"C808","照片"},index = 2)
        private File img;
    
        public UserExcel() {
        }
    }

     测试代码

    public class Test02 {
        public static void main(String[] args) {
            String fileName = "D:\\Java Code\\testmaven00\\src\\main\\java\\com\\test01Excel\\excel\\testExcel02.xlsx";
    
            EasyExcel.write(fileName, UserExcel.class).sheet("用户").doWrite(initData());
        }
    
        private static List<UserExcel> initData(){
            ArrayList<UserExcel> userList = new ArrayList<UserExcel>();
            UserExcel userExcel = new UserExcel();
    
            userExcel.setId(1);
            userExcel.setName("hzx");
            userExcel.setImg(new File("D:\\Java Code\\testmaven00\\src\\main\\java\\com\\test01Excel\\image\\Git配置.png"));
    
            userList.add(userExcel);
            return userList;
        }
    }

    效果


     设置单元格格式

    实体类

    public class UserExcel {
        @ExcelProperty(value = {"C808","序号"},index = 0)
        private Integer id;
        @ExcelProperty(value = {"C808","姓名"},index = 1)
        private String name;
        @ExcelProperty(value = {"C808","照片"},index = 2)
        private File img;
    
        public UserExcel() {
        }
    }

    设置全部单元格格式

    public class DefaultHandler {
    
        /**
         * 默认样式
         */
        public static HorizontalCellStyleStrategy defaultStyle(){
            // 标头样式
            WriteCellStyle headStyle = new WriteCellStyle();
            // 标头居中对齐
            headStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
            headStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
            // 表头字体
            WriteFont headFont = new WriteFont();
            headFont.setBold(true);
            headFont.setFontName("宋体");
            headFont.setFontHeightInPoints((short)12);
            headStyle.setWriteFont(headFont);
            headStyle.setBorderBottom(BorderStyle.THIN);
            headStyle.setBorderLeft(BorderStyle.THIN);
            headStyle.setBorderRight(BorderStyle.THIN);
            headStyle.setBorderTop(BorderStyle.THIN);
    
            // 内容样式
            WriteCellStyle contentStyle = new WriteCellStyle();
            // 背景为白色
            contentStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
            // 垂直居中
            contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            // 水平居中
            contentStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
    
            contentStyle.setWrapped(true);
            // 内容字体
            WriteFont contentFont = new WriteFont();
            contentFont.setFontName("宋体");
            contentFont.setFontHeightInPoints((short)11);
            contentStyle.setWriteFont(contentFont);
    
            // 初始化样式
            return new HorizontalCellStyleStrategy(headStyle, contentStyle);
        }
    }
    

     设置图片单元格

    public class ImageHandler extends AbstractCellWriteHandler {
    
        /**
         * 图片行列跨度
         */
        private int colSpan = 1;
        private int rowSpan = 1;
    
        /**
         * 左侧右侧边框粗细
         */
        private int borderPixelX1Y1 = 5;
        private int borderPixelX2Y2 = 5;
    
        /**
         * 可以随着单元格一起移动,改变大小
         */
        private ClientAnchor.AnchorType anchorType = ClientAnchor.AnchorType.MOVE_AND_RESIZE;
    
    
        /**
         * 单元格数据转换后调用
         */
        @Override
        public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
            // 不处理表头,不处理不含图片的
            boolean noImageValue = Objects.isNull(cellData)||Objects.isNull(cellData.getImageValue());
            if (Objects.equals(Boolean.TRUE,isHead)||noImageValue) {
                return;
            }
            // 设置单元格类型为EMPTY 让easyExcel不去处理该单元格
            cellData.setType(CellDataTypeEnum.EMPTY);
        }
    
        /**
         * 在单元格上的所有操作完成后调用
         */
        @Override
        public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
            if (cell.getColumnIndex()==2) {
                // 设置第三列的宽度
                writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(),10000);
            }
            if (!isHead) {
                // 设置首行外的列的高度
                writeSheetHolder.getSheet().getRow(cell.getRowIndex()).setHeight((short) 2000);
            }
            if (CollectionUtils.isEmpty(cellDataList)||Objects.equals(Boolean.TRUE,isHead)) {
                return;
            }
            CellData cellData = cellDataList.get(0);
            if (Objects.isNull(cellData)||Objects.isNull(cellData.getImageValue())) {
                return;
            }
            setImageValue(cellData,cell);
        }
    
        private void setImageValue(CellData cellData,Cell cell){
            Sheet sheet = cell.getSheet();
            int index = sheet.getWorkbook().addPicture(cellData.getImageValue(), XSSFWorkbook.PICTURE_TYPE_PNG);
            Drawing<?> drawing = sheet.getDrawingPatriarch();
            if (drawing == null) {
                drawing = sheet.createDrawingPatriarch();
            }
            CreationHelper helper = sheet.getWorkbook().getCreationHelper();
            ClientAnchor anchor = helper.createClientAnchor();
            // 图片边距
            final int borderWidth1 = Units.pixelToEMU(borderPixelX1Y1);
            final int borderWidth2 = Units.pixelToEMU(borderPixelX2Y2);
            // 图片左上角偏移量
            anchor.setDx1(borderWidth1);
            anchor.setDy1(borderWidth2);
            // 图片右下角偏移量
            anchor.setDx2(Math.negateExact(borderWidth1));
            anchor.setDy2(Math.negateExact(borderWidth2));
            // 图片行列
            anchor.setCol1(cell.getColumnIndex());
            anchor.setCol2(cell.getColumnIndex()+colSpan);
            anchor.setRow1(cell.getRowIndex());
            anchor.setRow2(cell.getRowIndex()+rowSpan);
            anchor.setAnchorType(anchorType);
            drawing.createPicture(anchor,index);
    
        }
    }
    

    测试代码

    public class Test03 {
    
        public static List<UserExcel> userExcelList;
    
        static {
            userExcelList = new ArrayList<UserExcel>();
            Collections.addAll(userExcelList,
                    new UserExcel(1,"hzx01",new File("D:\\Java Code\\testmaven00\\src\\main\\java\\com\\test01Excel\\image\\Git配置.png")),
                    new UserExcel(2,"hzx02",new File("D:\\Java Code\\testmaven00\\src\\main\\java\\com\\test01Excel\\image\\Git配置.png")),
                    new UserExcel(3,"hzx03",new File("D:\\Java Code\\testmaven00\\src\\main\\java\\com\\test01Excel\\image\\Git配置.png"))
                    );
        }
    
        public static void main(String[] args) {
            SimpleDateFormat sf = new SimpleDateFormat("HHmmss");
            String fileName = "D:\\Java Code\\testmaven00\\src\\main\\java\\com\\test01Excel\\excel\\testExcel03.xlsx";
            HorizontalCellStyleStrategy horizontalCellStyleStrategy = DefaultHandler.defaultStyle();
            // 设置默认样式
            ExcelWriterSheetBuilder builder = EasyExcel.write(fileName, UserExcel.class).sheet("列表").registerWriteHandler(horizontalCellStyleStrategy);
    
            builder.registerWriteHandler(new ImageHandler());
    
            builder.doWrite(userExcelList);
        }
    }

     效果


    动态表头

    之前通过实体类中的ExcelProperty注解规定表头的内容与位置,但如果要实现动态表头,则需要通过嵌套集合设置表头内容。

    实现代码

        private static List<List<String>> getHearder() {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String title = "C808:" + sf.format(new Date());
            List<List<String>> list = new ArrayList<>();
            List<String> head0 = new ArrayList<>();
            head0.add(title);
            head0.add("序号");
    
            List<String> head1 = new ArrayList<>();
            head1.add(title);
            head1.add("姓名");
    
            List<String> head2 = new ArrayList<>();
            head2.add(title);
            head2.add("照片");
    
            Collections.addAll(list, head0, head1, head2);
            return list;
        }

     效果


    多个sheet

    实体类

    public class UserExcel03 {
        private Integer id;
        private String name;
        private File img;
    
        public UserExcel03() {
        }
    }

    实现代码

    public class Test05 {
        private static List<UserExcel03> userExcelList;
    
        static {
            userExcelList = new ArrayList<UserExcel03>();
            Collections.addAll(userExcelList,
                    new UserExcel03(1, "hzx01", new File("D:\\Java Code\\testmaven00\\src\\main\\java\\com\\test01Excel\\image\\Git配置.png")),
                    new UserExcel03(2, "hzx02", new File("D:\\Java Code\\testmaven00\\src\\main\\java\\com\\test01Excel\\image\\Git配置.png")),
                    new UserExcel03(3, "hzx03"));
        }
    
        public static void main(String[] args) {
            String fileName = "D:\\Java Code\\testmaven00\\src\\main\\java\\com\\test01Excel\\excel\\testExcel05.xlsx";
            ExcelWriter excelWriter = null;
            HorizontalCellStyleStrategy horizontalCellStyleStrategy = DefaultHandler.defaultStyle();
            try {
                excelWriter = EasyExcel.write(fileName).build();
                // 实现多个sheet
                for (int i = 0; i <= 3; i++) {
                    WriteSheet writeSheet = null;
                    writeSheet = EasyExcel.writerSheet(i, "用户组" + i).head(getHearder()).registerWriteHandler(DefaultHandler.defaultStyle()).registerWriteHandler(new ImageHandler()).build();
                    excelWriter.write(userExcelList, writeSheet);
                }
            } finally {
                if (excelWriter != null) {
                    excelWriter.finish();
                }
            }
        }
    
        private static List<List<String>> getHearder() {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String title = "C808:" + sf.format(new Date());
            List<List<String>> list = new ArrayList<>();
            List<String> head0 = new ArrayList<>();
            head0.add(title);
            head0.add("序号");
    
            List<String> head1 = new ArrayList<>();
            head1.add(title);
            head1.add("姓名");
    
            List<String> head2 = new ArrayList<>();
            head2.add(title);
            head2.add("照片");
    
            Collections.addAll(list, head0, head1, head2);
            return list;
        }
    }
    

    效果


    一个sheet中有多个table 

    实体类

    public class UserExcel02 {
        @ExcelProperty(value = {"用户","序号"})
        private Integer id;
        @ExcelProperty(value = {"用户","姓名"})
        private String name;
    
        public UserExcel02() {
        }
    }
    public class StudentExcel {
        @ExcelProperty(value = {"学生","序号"},index = 0)
        private Integer id;
        @ExcelProperty(value = {"学生","姓名"},index = 1)
        private String studentName;
        @ExcelProperty(value = {"学生","性别"},index = 2)
        private String sex;
    }

    实现代码

    public class Test06 {
        private static List<UserExcel02> userExcelList;
    
        private static List<StudentExcel> studentExcelList;
    
        static {
            userExcelList = new ArrayList<UserExcel02>();
            Collections.addAll(userExcelList, new UserExcel02(1, "hzx01"), new UserExcel02(2, "hzx02"), new UserExcel02(3, "hzx03"), new UserExcel02());
    
            studentExcelList = new ArrayList<>();
            Collections.addAll(studentExcelList, new StudentExcel(1, "stu000000000000000001", "男"), new StudentExcel(2, "stu02", "女"), new StudentExcel());
        }
    
        public static void main(String[] args) {
            String fileName = "D:\\Java Code\\testmaven00\\src\\main\\java\\com\\test01Excel\\excel\\testExcel06.xlsx";
            ExcelWriter excelWriter = null;
            HorizontalCellStyleStrategy horizontalCellStyleStrategy = DefaultHandler.defaultStyle();
            try {
                excelWriter = EasyExcel.write(fileName).build();
                WriteSheet writeSheet = null;
                // sheet0
                writeSheet = EasyExcel.writerSheet(0, "总览").build();
                WriteTable writeTable0 = EasyExcel.writerTable(0).head(UserExcel02.class).registerWriteHandler(DefaultHandler.defaultStyle()).build();
                WriteTable writeTable1 = EasyExcel.writerTable(1).head(StudentExcel.class).registerWriteHandler(DefaultHandler.defaultStyle()).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
                excelWriter.write(userExcelList, writeSheet, writeTable0);
                excelWriter.write(studentExcelList, writeSheet, writeTable1);
            } finally {
                if (excelWriter != null) {
                    excelWriter.finish();
                }
            }
        }
    }
    

    效果


    合并单元格

    实体类

    public class UserExcel02 {
        @ExcelProperty(value = {"用户","序号"})
        private Integer id;
        @ExcelProperty(value = {"用户","姓名"})
        private String name;
    
        public UserExcel02() {
        }
    }
    public class StudentExcel {
        @ExcelProperty(value = {"学生","序号"},index = 0)
        private Integer id;
        @ExcelProperty(value = {"学生","姓名"},index = 1)
        private String studentName;
        @ExcelProperty(value = {"学生","性别"},index = 2)
        private String sex;
    }

    单元格合并

    public class CellMergeHandler extends AbstractMergeStrategy {
    
        // 合并坐标集合
        private List<CellRangeAddress> cellRangeAddress;
    
        public CellMergeHandler(List<CellRangeAddress> cellRangeAddress) {
            this.cellRangeAddress = cellRangeAddress;
        }
    
        @Override
        protected void merge(Sheet sheet, Cell cell, Head head, Integer integer) {
            if (!CollectionUtils.isEmpty(cellRangeAddress)) {
                for (CellRangeAddress item : cellRangeAddress) {
                    sheet.addMergedRegionUnsafe(item);
                }
            }
        }
    }
    

     实现代码

    public class Test07 {
        private static List<UserExcel02> userExcelList;
    
        private static List<StudentExcel> studentExcelList;
    
        static {
            userExcelList = new ArrayList<UserExcel02>();
            Collections.addAll(userExcelList, new UserExcel02(1, "hzx01"), new UserExcel02(2, "hzx02"), new UserExcel02(3, "hzx03"), new UserExcel02());
    
            studentExcelList = new ArrayList<>();
            Collections.addAll(studentExcelList, new StudentExcel(1, "stu000000000000000001", "男"), new StudentExcel(2, "stu02", "女"), new StudentExcel());
        }
    
        public static void main(String[] args) {
            String fileName = "D:\\Java Code\\testmaven00\\src\\main\\java\\com\\test01Excel\\excel\\testExcel07.xlsx";
            ExcelWriter excelWriter = null;
    
            // 设置合并的单元格
            List<CellRangeAddress> cellRangeList = new ArrayList<>();
            cellRangeList.add(new CellRangeAddress(0, 0, 1, 2));
            cellRangeList.add(new CellRangeAddress(1, 1, 1, 2));
            cellRangeList.add(new CellRangeAddress(2, 2, 1, 2));
            cellRangeList.add(new CellRangeAddress(3, 3, 1, 2));
            cellRangeList.add(new CellRangeAddress(4, 4, 1, 2));
    
            try {
                excelWriter = EasyExcel.write(fileName).build();
                WriteSheet writeSheet = null;
                // sheet0
                writeSheet = EasyExcel.writerSheet(0, "总览").build();
                WriteTable writeTable0 = EasyExcel.writerTable(0).head(UserExcel02.class).registerWriteHandler(DefaultHandler.defaultStyle()).registerWriteHandler(new CellMergeHandler(cellRangeList)).build();
                WriteTable writeTable1 = EasyExcel.writerTable(1).head(StudentExcel.class).registerWriteHandler(DefaultHandler.defaultStyle()).build();
                excelWriter.write(userExcelList, writeSheet, writeTable0);
                excelWriter.write(studentExcelList, writeSheet, writeTable1);
            } finally {
                if (excelWriter != null) {
                    excelWriter.finish();
                }
            }
        }
    }
    

    效果

    展开全文
  • java生成Excel文件

    万次阅读 2020-12-13 19:16:35
    添加依赖 <!-- 使用 xls 格式 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version>... .
  • java生成excel文件,以及导出

    千次阅读 2022-04-22 10:22:30
    java实现导入导出excel
  • 主要介绍了Java实现读取及生成Excel文件的方法,结合实例形式分析了java通过引入第三方jar包poi-3.0.1-FINAL-20070705.jar实现针对Excel文件的读取及生成功能,需要的朋友可以参考下
  • Java 创建Excel散点图

    2021-01-07 14:27:34
    Java 创建Excel散点图 散点图是指在回归分析中,数据点在直角坐标系平面上的分布图,表示因变量随自变量而变化的大致趋势,常用于比较跨类别的聚合数据。在excel中,支持创建散点图来实现数据分析,本文将通过java...
  • Java创建写一个Excel文件

    千次阅读 2018-09-04 15:31:43
    下面直接上代码了啊亲~~ ...import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet;...
  • java 生成Excel文件

    2010-06-21 08:52:17
    java 生成Excel文件 java 生成Excel文件 java 生成Excel文件
  • java实现创建excel表并导出到本地

    万次阅读 多人点赞 2018-07-19 20:56:31
    1. 创建Member类,此类用来创建对象,存储在excel表格中的数据 package com.yu.bai.excel; import java.util.Date; //创建一个实体对象,用来存储到excel表中 public class Member { private Integer code; ...
  • 作者:悠悠做神仙 来源: 恒生LIGHT云社区 该部分主要是在做testNG...1、利用POI创建excel写入数据 首先,导入依赖,在pom文件增加以下依赖: <dependency> <groupId>org.apache.poi</groupId> .
  • Java读写excel文件代码

    千次阅读 2022-03-25 11:31:04
    教你如何使用简单java代码读写excel文件 ***提示:最基础教程代码教学 学习内容 在学习或者工作中,Excel文件作为最基础的办公表格,会被频繁使用。很多情况下,Excel里自带的公式或者函数已经不能简单满足我们的...
  • java解析Excel文件

    千次阅读 2021-12-25 11:12:58
    下文介绍java解析Excel文件的方案前置准备1.第三方jar包或者Maven配置org.apache.poi的jar包Maven配置如下 <groupId>org.apache.poi <artifactId>poi <version>3.15</dependency>分析首先...
  • Java用POI实现根据Excel表格模板生成新的Excel并实现数据输出...
  • java读取excel文件

    千次阅读 2021-06-24 10:01:56
    java读取excel文件有相应的插件:jxl、easyexcel、poi 今天来讲jxl与easyexcel的实战! jxl读取excel,只能是2003版的excel 读取2007 版的excel会报错 此时,如果你的excel是07版的,那么可以另存为03版的 jxl...
  • java生成excel表格

    2015-12-29 13:16:38
    java生成excel表格代码示例,下载后可以直接使用
  • java jxl 通过excel模板创建文件并将数据导入excel,实现按行、列插入数据
  • JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容、创建新的Excel文件、更新已经存在的Excel文件。使用该API非Windows操作系统也可以通过纯Java应用来处理Excel数据表。因为它是使用...
  • java 实现导出Excel文件

    2021-03-06 15:55:26
    java 实现导出Excel(java生成 excel 并导出文件)经常有有一些数据需要导出成 excel 格式 ,所以就需要实现啦开始:1.加入jarpoi-3.6-20091214.jarcommons-... 实现代码 :1 /**2 * 创建excel3 *4 *@paramlist5 *@pa...
  • Java文件处理:Excel 文档创建、写入和读取
  • 原因:我们经常会遇到甲方给数据是excel格式的数据,当然Navicat是...* @param 上传excel文件读取excel数据并保存 * * @return * @throws IOException */ @RequestMapping(value = "batchUploadExcel") public voi
  • 使用POI官网上的事件驱动模式的示例方法,读取单sheet的Excel表格文件(.xlsx),Microsoft Excel和WPS Excel创建的表格文件可以正常读取数据,但是java代码创建的表格文件(不使用软件打开并保存)却读取不到数据。...
  • java创建临时文件

    千次阅读 2021-02-12 09:47:20
    createTempFile(String prefix, String suffix)在默认临时文件目录中创建一个空文件,使用给定前缀和后缀生成其名称。createTempFile(String prefix, String suffix, File directory)在指定目录中创建一个新的空文件...
  • java读取excel文件并写入.txt文件中

    千次阅读 2019-08-22 17:33:46
    logger.info("无法转换的Excel文件格式(后缀名应为:.xls或.xlsx)"); } } catch (FileNotFoundException e) { logger.info("找不到指定文件"); } catch (IOException e) { logger.info("IO异常"); } } /** ...
  • Java读取批量Excel文件

    千次阅读 2020-04-29 16:38:23
    Java读取批量Excel文件,并转化为List<Map<String,String>> 1.首先基础知识: 一 :简介 开发中经常会设计到excel的处理,如导出Excel,导入Excel到数据库中,操作Excel目前有两个框架,一个是...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 63,199
精华内容 25,279
关键字:

java创建excel文件