zoukankan      html  css  js  c++  java
  • Java通过POI对表格文件写入数据

    一、下载并导入相应版本的包(根据Java版本)

    下载地址:https://archive.apache.org/dist/poi/release/bin/

    需要导入的包有:

     压缩文件根目录下的5个包,lib文件下的3个包

    二、新建表格

    public static void main(String[] args) throws IOException{
            Workbook wb=new HSSFWorkbook();//新建一个工作簿
                    FileOutputStream fout=new FileOutputStream("F:\poi.xls");
                    wb.write(fout);//Workbook提供了write的方法
            fout.close();//将输出流关闭
    }

    三、新建sheet页

    public static void main(String[] args) throws IOException{
            Workbook wb=new HSSFWorkbook();//新建一个工作簿
            FileOutputStream fout=new FileOutputStream("F:\poi.xls");
            Sheet sheet1 = wb.createSheet("第一个sheet页");//创建一个sheet页
            Sheet sheet2 = wb.createSheet("第二个sheet页");//创建第二个sheet页
            wb.write(fout);//Workbook提供了write的方法
            fout.close();//将输出流关闭
        }

    四、依次创建行、单元格

    public static void main(String[] args) throws IOException{
            Workbook wb=new HSSFWorkbook();//新建一个工作簿
            FileOutputStream fout=new FileOutputStream("F:\poi.xls");
            Sheet sheet1 = wb.createSheet("第一个sheet页");//创建一个sheet页

    Row row=sheet1.createRow(0); Cell cell=row.createCell(0); cell.setCellValue(1); row.createCell(1).setCellValue(1.2);//创建第一行第二个单元格 row.createCell(2).setCellValue("这是一个单元格");//创建第一行第三个单元格 row.createCell(3).setCellValue(false);//创建第一行第四个单元格 row.createCell(4).setCellValue(HSSFCell.ENCODING_COMPRESSED_UNICODE); Row row1 = sheet1.createRow(1); row1.createCell(0).setCellValue("第二行第一列"); row1.createCell(1).setCellValue(true); row1.createCell(2).setCellValue("第二行第三列"); row1.createCell(3).setCellValue("第二行第四列"); Sheet sheet2 = wb.createSheet("第二个sheet页");//创建第二个sheet页 wb.write(fout);//Workbook提供了write的方法 fout.close();//将输出流关闭 }
  • 相关阅读:
    七easy网络陷阱上当
    移动端--web开展
    ContentType是否大小写区分?
    NYOJ 24 素数的距离问题
    Emoji:搜索将与您找到表情符号背后的故事
    Cocos2d-X之LUA注意事项
    [Angular] ChangeDetection -- onPush
    [Node.js] Build microservices in Node.js with micro
    [Angular] Scrolling the Message List To the Bottom Automatically Using OnChanges
    [Angular] Ngrx/effects, Action trigger another action
  • 原文地址:https://www.cnblogs.com/hehejeson/p/12493741.html
Copyright © 2011-2022 走看看