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();//将输出流关闭 }
  • 相关阅读:
    Neko's loop HDU-6444(网络赛1007)
    Parameters
    SETLOCAL
    RD / RMDIR Command
    devenv 命令用法
    Cannot determine the location of the VS Common Tools folder.
    'DEVENV' is not recognized as an internal or external command,
    How to change Visual Studio default environment setting
    error signing assembly unknown error
    What is the Xcopy Command?:
  • 原文地址:https://www.cnblogs.com/hehejeson/p/12493741.html
Copyright © 2011-2022 走看看