Java 生成一个excel表 9*9

工具

Apache 提供的POI,可以对Microsoft Excel, Word, PPT, Visio进行文件创建、读取等功能

实验目的

创建一个excel文件,内容是9*9表格
Java 生成一个excel表 9*9_第1张图片

代码

java
public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\workbook.xlsx");
        String letter = "ABCDEFGHIJK";
        String joint;
        Workbook wb = new XSSFWorkbook(); 
        Sheet sheet1 = wb.createSheet("Sheet1");

        CellStyle style = wb.createCellStyle(); //单元格样式
        style.setAlignment(CellStyle.ALIGN_CENTER); //水平居中
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //垂直居中

        for(int r = 0; r < 9; r++){
            Row row = sheet1.createRow(r);
            for(int c = 0; c < r + 1; c ++){
                joint = letter.charAt(r) + "" + (c + 1);
                Cell cell = row.createCell(c);
                cell.setCellStyle(style);
                //往cell写入公式
                cell.setCellFormula("ROW(" + joint + ")&\"*\"&COLUMN(" + joint + ")&\"=\"&ROW(" + joint + ")*COLUMN(" + joint + ")");

            }
        }

        //设置列宽
        for(int i = 0; i < 9; i++)
            sheet1.setColumnWidth(i, 10 * 256);

        wb.write(fos);
        fos.close();
    }

你可能感兴趣的:(•,Java)