使用Apache POI HSSF从Excel图表数据系列中读取系列值

时间:2022-08-09 20:23:16

I want to extract the actual series data and values from a chart in xls file using Apache POI. Point Values like the pair (15.44956728, 7) as shown below. I managed to extract the title of the chart but could not do it with the needed data. Here is my code:

我想使用Apache POI从xls文件中的图表中提取实际的系列数据和值。点值如对(15.44956728,7),如下所示。我设法提取图表的标题,但无法使用所需的数据。这是我的代码:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFChart;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //System.out.println("Hello, World");
        InputStream inp;
        try {
            inp = new FileInputStream("USRAK_00017_0.xls");
            HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
            ExcelExtractor extractor = new ExcelExtractor(wb);
            extractor.setFormulasNotResults(true);
            extractor.setIncludeSheetNames(true);
            String text = extractor.getText();
            //System.out.println(text);
            HSSFSheet sheet = wb.getSheetAt(0);
            HSSFChart[] sheetCharts = HSSFChart.getSheetCharts(sheet);
            System.out.println(sheetCharts[0].getSeries()[0].getSeriesTitle());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

使用Apache POI HSSF从Excel图表数据系列中读取系列值

1 个解决方案

#1


0  

A better approach I found is to dump the XLS file to XML content in fods format. Open Office can do this job via the following command:

我发现一种更好的方法是将XLS文件转换为fods格式的XML内容。 Open Office可以通过以下命令完成此任务:

soffice --headless --convert-to fods USRAK_00017_0.xls

Then you can parse the XML the way you like and parse the part you want to extract from the chart

然后,您可以按照自己喜欢的方式解析XML,并解析要从图表中提取的部分

#1


0  

A better approach I found is to dump the XLS file to XML content in fods format. Open Office can do this job via the following command:

我发现一种更好的方法是将XLS文件转换为fods格式的XML内容。 Open Office可以通过以下命令完成此任务:

soffice --headless --convert-to fods USRAK_00017_0.xls

Then you can parse the XML the way you like and parse the part you want to extract from the chart

然后,您可以按照自己喜欢的方式解析XML,并解析要从图表中提取的部分