使用Apache POI在Java中读/写Excel文件时出现问题

使用Apache POI在Java中读/写Excel文件时出现问题,java,loops,apache-poi,Java,Loops,Apache Poi,大家好,我是java poi库的新手,我尝试了所有的运气来学习这个库,但仍然没有运气 我想要这个输出 Excel1.xls有此数据 邮政编码|地点|日期 211 我想复制所有第一行数据 邮编|地点|日期 然后把它放到另一张纸上 这是我制作的代码 public static void main(String[] args) throws IOException{ try { FileInputStream file = new FileInputStream(new Fi

大家好,我是java poi库的新手,我尝试了所有的运气来学习这个库,但仍然没有运气

我想要这个输出

Excel1.xls有此数据

邮政编码|地点|日期
211

我想复制所有第一行数据

邮编|地点|日期

然后把它放到另一张纸上

这是我制作的代码

public static void main(String[] args) throws IOException{

    try {
        FileInputStream file = new FileInputStream(new File("d:\\input.xls"));

        HSSFWorkbook workbook = new HSSFWorkbook(file);
        HSSFSheet sheet = workbook.getSheetAt(0);
        HSSFSheet zip1 = workbook.createSheet("ZIP CODE 1");


        for(Row row : sheet){
            int i=0;

            for(Cell cell : row){

                cell.setCellType(Cell.CELL_TYPE_STRING);
                System.out.print(cell.getStringCellValue() + "\t");
                String a = cell.getStringCellValue();

                cell = zip1.createRow(i).createCell(i);

                i++;
                cell.setCellValue(a);
             }
             break;

         }

        file.close();
        FileOutputStream outFile =new FileOutputStream(new File("d:\\output.xls"));
        workbook.write(outFile);
        outFile.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
发现的问题

  • 对于.xlsx文件->
    HSSFWorkbook
    应为
    XSSFWorkbook

  • 循环。您不想循环每一行,只想循环第一行。只需循环列

  • 不要每次写入单元格时都创建行。只创建一次新行

  • 工作示例:

    try {
        FileInputStream file = new FileInputStream(new File(
                "C:\\path\\Book1.xlsx"));
    
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        XSSFSheet sheet = workbook.getSheetAt(0);
        XSSFSheet zip1 = workbook.createSheet("ZIP CODE 1");
    
        Row readFirstRow = sheet.getRow(0);
        Row writeFirstRow = zip1.createRow(0);
    
        for (Cell cell : readFirstRow) {
    
            cell.setCellType(Cell.CELL_TYPE_STRING);
            String a = cell.getStringCellValue();
    
            cell = writeFirstRow.createCell(cell.getColumnIndex());
            cell.setCellValue(a);
        }
    
        file.close();
        FileOutputStream outFile = new FileOutputStream(new File(
                "C:\\path\\BookOut.xlsx"));
        workbook.write(outFile);
        outFile.close();
    
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    发现的问题

  • 对于.xlsx文件->
    HSSFWorkbook
    应为
    XSSFWorkbook

  • 循环。您不想循环每一行,只想循环第一行。只需循环列

  • 不要每次写入单元格时都创建行。只创建一次新行

  • 工作示例:

    try {
        FileInputStream file = new FileInputStream(new File(
                "C:\\path\\Book1.xlsx"));
    
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        XSSFSheet sheet = workbook.getSheetAt(0);
        XSSFSheet zip1 = workbook.createSheet("ZIP CODE 1");
    
        Row readFirstRow = sheet.getRow(0);
        Row writeFirstRow = zip1.createRow(0);
    
        for (Cell cell : readFirstRow) {
    
            cell.setCellType(Cell.CELL_TYPE_STRING);
            String a = cell.getStringCellValue();
    
            cell = writeFirstRow.createCell(cell.getColumnIndex());
            cell.setCellValue(a);
        }
    
        file.close();
        FileOutputStream outFile = new FileOutputStream(new File(
                "C:\\path\\BookOut.xlsx"));
        workbook.write(outFile);
        outFile.close();
    
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    发现的问题

  • 对于.xlsx文件->
    HSSFWorkbook
    应为
    XSSFWorkbook

  • 循环。您不想循环每一行,只想循环第一行。只需循环列

  • 不要每次写入单元格时都创建行。只创建一次新行

  • 工作示例:

    try {
        FileInputStream file = new FileInputStream(new File(
                "C:\\path\\Book1.xlsx"));
    
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        XSSFSheet sheet = workbook.getSheetAt(0);
        XSSFSheet zip1 = workbook.createSheet("ZIP CODE 1");
    
        Row readFirstRow = sheet.getRow(0);
        Row writeFirstRow = zip1.createRow(0);
    
        for (Cell cell : readFirstRow) {
    
            cell.setCellType(Cell.CELL_TYPE_STRING);
            String a = cell.getStringCellValue();
    
            cell = writeFirstRow.createCell(cell.getColumnIndex());
            cell.setCellValue(a);
        }
    
        file.close();
        FileOutputStream outFile = new FileOutputStream(new File(
                "C:\\path\\BookOut.xlsx"));
        workbook.write(outFile);
        outFile.close();
    
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    发现的问题

  • 对于.xlsx文件->
    HSSFWorkbook
    应为
    XSSFWorkbook

  • 循环。您不想循环每一行,只想循环第一行。只需循环列

  • 不要每次写入单元格时都创建行。只创建一次新行

  • 工作示例:

    try {
        FileInputStream file = new FileInputStream(new File(
                "C:\\path\\Book1.xlsx"));
    
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        XSSFSheet sheet = workbook.getSheetAt(0);
        XSSFSheet zip1 = workbook.createSheet("ZIP CODE 1");
    
        Row readFirstRow = sheet.getRow(0);
        Row writeFirstRow = zip1.createRow(0);
    
        for (Cell cell : readFirstRow) {
    
            cell.setCellType(Cell.CELL_TYPE_STRING);
            String a = cell.getStringCellValue();
    
            cell = writeFirstRow.createCell(cell.getColumnIndex());
            cell.setCellValue(a);
        }
    
        file.close();
        FileOutputStream outFile = new FileOutputStream(new File(
                "C:\\path\\BookOut.xlsx"));
        workbook.write(outFile);
        outFile.close();
    
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    尝试使用Xcelite
    https://github.com/eBay/xcelite#writing
    写:
    公共类用户{
    @列(name=“Firstname”)
    私有字符串名;
    @列(name=“Lastname”)
    私有字符串lastName;
    @纵队
    私人长id;
    @纵队
    私人生日;
    }
    Xcelite Xcelite=新的Xcelite();
    XceliteSheet sheet=xcelite.createSheet(“用户”);
    SheetWriter writer=sheet.getBeanWriter(User.class);
    列表用户=新建ArrayList();
    //…填充用户
    writer.write(用户);
    写入(新文件(“users_doc.xlsx”);
    
    尝试使用Xcelite
    https://github.com/eBay/xcelite#writing
    写:
    公共类用户{
    @列(name=“Firstname”)
    私有字符串名;
    @列(name=“Lastname”)
    私有字符串lastName;
    @纵队
    私人长id;
    @纵队
    私人生日;
    }
    Xcelite Xcelite=新的Xcelite();
    XceliteSheet sheet=xcelite.createSheet(“用户”);
    SheetWriter writer=sheet.getBeanWriter(User.class);
    列表用户=新建ArrayList();
    //…填充用户
    writer.write(用户);
    写入(新文件(“users_doc.xlsx”);
    
    尝试使用Xcelite
    https://github.com/eBay/xcelite#writing
    写:
    公共类用户{
    @列(name=“Firstname”)
    私有字符串名;
    @列(name=“Lastname”)
    私有字符串lastName;
    @纵队
    私人长id;
    @纵队
    私人生日;
    }
    Xcelite Xcelite=新的Xcelite();
    XceliteSheet sheet=xcelite.createSheet(“用户”);
    SheetWriter writer=sheet.getBeanWriter(User.class);
    列表用户=新建ArrayList();
    //…填充用户
    writer.write(用户);
    写入(新文件(“users_doc.xlsx”);
    
    尝试使用Xcelite
    https://github.com/eBay/xcelite#writing
    写:
    公共类用户{
    @列(name=“Firstname”)
    私有字符串名;
    @列(name=“Lastname”)
    私有字符串lastName;
    @纵队
    私人长id;
    @纵队
    私人生日;
    }
    Xcelite Xcelite=新的Xcelite();
    XceliteSheet sheet=xcelite.createSheet(“用户”);
    SheetWriter writer=sheet.getBeanWriter(User.class);
    列表用户=新建ArrayList();
    //…填充用户
    writer.write(用户);
    写入(新文件(“users_doc.xlsx”);
    
    谢谢你,希瑟姆!:D你的代码看起来很简单^ ^我希望我能像你一样擅长编程再次感谢你^ ^谢谢你hitham!:D你的代码看起来很简单^ ^我希望我能像你一样擅长编程再次感谢你^ ^谢谢你hitham!:D你的代码看起来很简单^ ^我希望我能像你一样擅长编程再次感谢你^ ^谢谢你hitham!:D你的代码看起来很简单^我希望我能像你一样擅长编程再次感谢你^_^