• 基于Apache POI 向xlsx写入数据


    【0】写在前面


    package com.cwind.poi;  
    import java.io.FileOutputStream;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    /**
     * @author Billy Chen
     */
    public class SimpleDatasheetWriter {
    
        private static final String[] titles = {
                "姓名", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
    
        //sample data to fill the sheet.
        private static final String[][] data = {
                {"AngelaBaby", "跑了", "跑了", "跑了", "跑了", "跑了"},
                {"邓超", "跑了", "跑了", "没跑", "跑了", "跑了" },
                {"王祖蓝", "没跑", "没跑", "没跑", "跑了", "跑了" },
                {"王宝强", "跑了", "跑了", "跑了", "跑了", "跑了" },
                {"郑恺", "跑了", "跑了", "跑了", "跑了", "跑了" }
        };
    
        public static void main(String[] args) throws Exception {
    
            Workbook wb;
    

    //创建工作簿

            if(args.length > 0 && args[0].equals("-xls")) 
                wb = new HSSFWorkbook();
            else
                wb = new XSSFWorkbook();
    

    //创建名为 Running Man 的纸张

            Sheet sheet = wb.createSheet("Running Man");
    

    //创建行坐标为0的行(为什么管它叫坐标,不把它叫做行,呵呵)

            Row headerRow = sheet.createRow(0);
    

    //设置行高

            headerRow.setHeightInPoints(12.75f);
    
            for (int i = 0; i < titles.length; i++) {
    

    //创建行坐标为0的单元格,且其列坐标为i;

                Cell cell = headerRow.createCell(i);
    

    //设置单元格的value

                cell.setCellValue(titles[i]); 
            }
    
            Row row;
            Cell cell;
            int rownum = 1;
            for (int i = 0; i < data.length; i++, rownum++) {
    

    //创建行坐标为rownum 的行

                row = sheet.createRow(rownum); 
                if(data[i] == null) continue;
    
                for (int j = 0; j < data[i].length; j++) {
    

    // 为行创建单元格;

                    cell = row.createCell(j); 
    

    // 设置单元格的value

                    cell.setCellValue(data[i][j]);            
                     }
            }
    
            System.out.println("Default column  " + sheet.getRow(0).getLastCellNum());
            System.out.println("Default column  " + sheet.getRow(0).getPhysicalNumberOfCells());
    

    // Write the output to a file

            String file = "E:/bench-cluster/temp-resource/RunningMan.xlsx";
            if(wb instanceof XSSFWorkbook) file += "x";
    
            FileOutputStream out = new FileOutputStream(file);
            wb.write(out);
            out.close();
    //        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        }
    }
    
  • 相关阅读:
    zookeeper端口号冲突:8080冲突
    Linux 开机报 or type Control-D to continue
    linux分区和系统文件和挂载
    linux添加JAVA环境变量
    root用户安装的软件在普通用户不生效
    微信小程序:text元素中加入空格
    Java Swing:JPanel添加边框
    Java Swing:JPanel中添加JPanel
    Spring Boot:项目打包成war并发布到Tomcat上运行
    IDEA将MAVEN项目打包成war包
  • 原文地址:https://www.cnblogs.com/pacoson/p/4893164.html
Copyright © 2020-2023  润新知