Java Apache POI 读取包含多个工作表的 excel 文件并将单个工作表写入新文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23814579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:22:37  来源:igfitidea点击:

Apache POI to read excel file with many worksheets and write single sheet to new file

javaexcelapache-poi

提问by user3665150

I am using Apache POI to read an xlsx file that has many worksheets in it. I want to write out the contents to a new xlsx file, but I only want one selected worksheet from the original input.

我正在使用 Apache POI 读取包含许多工作表的 xlsx 文件。我想将内容写到一个新的 xlsx 文件中,但我只想要从原始输入中选择一个工作表。

The example I have (code below) reads the file fine, but it writes out all worksheets to the file. I need to know what to do to write only one of the sheets. Can I do that?

我的示例(下面的代码)可以很好地读取文件,但它将所有工作表写入文件。我需要知道该怎么做才能只写一张纸。我可以这样做吗?

import java.io.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelTest {

    public static void main(String[] args)
    {  

    try
    {
        FileInputStream file = new FileInputStream(new File("FileLotOfSheets.xlsx"));
        //Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook = new XSSFWorkbook(file);

        //Get first/desired sheet from the workbook
       // XSSFSheet sheet = workbook.getSheetAt(0);

        file.close();

        FileOutputStream out = new FileOutputStream("FileOnlyOneSheetFromLots.xlsx");
        workbook.write(out);
        out.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    }
}

回答by user3665150

public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
        FileInputStream file = new FileInputStream(new File(
                "FileLotOfSheets.xlsx"));
        // Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook = new XSSFWorkbook(file);

        /* ------------------------------------------------------------ 
         * As per suggestion from rgettman to remove all but desired sheet,
         * added the following lines to get rid of the unwanted sheets.
         * Result is workbook with only the sheet I want.
         * ------------------------------------------------------------- */
        String sheetName = "mySheet";
        for (int i = workbook.getNumberOfSheets() - 1; i >= 0; i--) {
            XSSFSheet tmpSheet = workbook.getSheetAt(i);
            if (!tmpSheet.getSheetName().equals(sheetName)) {
                workbook.removeSheetAt(i);
            }
        }
        /*----------------------------------------------------------------*/

        file.close();

        FileOutputStream out = new FileOutputStream(
                "FileOnlyOneSheetFromLots.xlsx");
        workbook.write(out);
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

回答by kallayya Hiremath

Consider the following version of main

考虑以下版本 main

 public static void main( String [] args ) {
  try {

    InputStream input = POIExample.class.getResourceAsStream( "qa.xls" );
    POIFSFileSystem fs = new POIFSFileSystem( input );
    HSSFWorkbook wb = new HSSFWorkbook(fs);


    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        HSSFSheet sheet = wb.getSheetAt(i);

        // Do your stuff        
    }

} catch ( IOException ex ) {
    ex.printStackTrace();
}
}