简体   繁体   English

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

[英]Read Series Values from Excel Chart Data Series Using Apache POI HSSF

I want to extract the actual series data and values from a chart in xls file using Apache POI. 我想使用Apache POI从xls文件中的图表中提取实际系列数据和值。 Point Values like the pair (15.44956728, 7) as shown below. 点值类似于一对(15.44956728,7),如下所示。 I managed to extract the title of the chart but could not do it with the needed data. 我设法提取了图表的标题,但无法使用所需的数据。 Here is my code: 这是我的代码:

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();
        }
    }
}

在此处输入图片说明

A better approach I found is to dump the XLS file to XML content in fods format. 我发现一种更好的方法是将XLS文件以fods格式转储为XML内容。 Open Office can do this job via the following command: 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并解析要从图表中提取的部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM