Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Apache POI更改Excel折线图中的数据范围_Java_Charts_Apache Poi_Xssf - Fatal编程技术网

Java 使用Apache POI更改Excel折线图中的数据范围

Java 使用Apache POI更改Excel折线图中的数据范围,java,charts,apache-poi,xssf,Java,Charts,Apache Poi,Xssf,我试图看看是否可以使用ApachePOI更改折线图中系列的数据范围 我能够从图表本身中提取序列,但找不到允许我更改数据范围的方法 XSSFWorkbook workbook = new XSSFWorkbook("C:\\Workbook.xlsx"); Sheet worksheet = workbook.getSheetAt(0); XSSFDrawing drawing = (XSSFDrawing) worksheet.createDrawingPatriarch(); List<

我试图看看是否可以使用ApachePOI更改折线图中系列的数据范围

我能够从图表本身中提取序列,但找不到允许我更改数据范围的方法

XSSFWorkbook workbook = new XSSFWorkbook("C:\\Workbook.xlsx");
Sheet worksheet = workbook.getSheetAt(0);
XSSFDrawing drawing = (XSSFDrawing) worksheet.createDrawingPatriarch();
List<XSSFChart> charts = drawing.getCharts();
for (XSSFChart chart : charts) {
    String title = chart.getTitleText().toString();
    if (title.equals("Z-Acceleration")) {
        CTChart cc = chart.getCTChart();
        CTPlotArea plotArea = cc.getPlotArea();
        CTLineSer[] ccc = plotArea.getLineChartArray()[0].getSerArray();
        for (CTLineSer s : ccc) {
            System.out.println(s.xmlText());
        }
        System.out.println(ccc.length);
    }
}
xssf工作簿=新的xssf工作簿(“C:\\workbook.xlsx”);
工作表=工作簿。getSheetAt(0);
XSSFDrawing=(XSSFDrawing)工作表。CreateDrawingParacher();
列表图表=drawing.getCharts();
用于(XSSFChart图表:图表){
字符串标题=chart.getTitletText().toString();
if(标题等于(“Z加速度”)){
CTChart cc=chart.getCTChart();
CTPlotArea plotArea=cc.getPlotArea();
CTLineSer[]ccc=plotArea.getLineChartArray()[0].GetSerray();
用于(CTLineSer s:ccc){
System.out.println(s.xmlText());
}
系统输出打印长度(ccc长度);
}
}

我打印了XML文本,以查看它是否确实能够正确地从图表中提取该系列,是否能够找到其标题和数据范围,但无法对其进行更改。

好的,因为这是一个很好的问题,让我们举一个具体的示例,说明如何使用
apache poi
更改Excel折线图中的数据范围

让我们从以下表格开始:

然后输入以下代码:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.SpreadsheetVersion;

import org.openxmlformats.schemas.drawingml.x2006.chart.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.util.List;

class ExcelChangeChartDataSource {

 static XSSFChart getChartWithTitle(XSSFSheet sheet, String wantedTitle) {
  if (sheet == null || wantedTitle == null) return null;
  XSSFDrawing drawing = sheet.createDrawingPatriarch();
  List<XSSFChart> charts = drawing.getCharts();
  for (XSSFChart chart : charts) {
   String title = chart.getTitleText().toString();
   if (wantedTitle.equals(title)) return chart;
  }
  return null;
 }

 static void addMonthDataToChart(XSSFSheet sheet, XSSFChart chart, String month, Double[] seriesData) {
  CTChart ctChart = chart.getCTChart();
  CTPlotArea ctPlotArea = ctChart.getPlotArea();
  List<CTLineSer> ctLineSerList = ctPlotArea.getLineChartArray(0).getSerList();

  Row row;
  Cell cell;
  int ser = 0;
  for (CTLineSer ctLineSer : ctLineSerList) {

   CTAxDataSource cttAxDataSource = ctLineSer.getCat();
   CTStrRef ctStrRef = cttAxDataSource.getStrRef();

   AreaReference catReference = new AreaReference(ctStrRef.getF(), SpreadsheetVersion.EXCEL2007);
   CellReference firstCatCell = catReference.getFirstCell();
   CellReference lastCatCell = catReference.getLastCell();
   if (firstCatCell.getCol() == lastCatCell.getCol()) {
    int col = firstCatCell.getCol();
    int lastRow = lastCatCell.getRow();
    row = sheet.getRow(lastRow+1); if (row == null) row = sheet.createRow(lastRow+1);
    cell = row.getCell(col); if (cell == null) cell = row.createCell(col);
    cell.setCellValue(month);

    ctStrRef.setF(new AreaReference(
                  firstCatCell, 
                  new CellReference(lastCatCell.getSheetName(), lastRow+1, col, true, true), 
                  SpreadsheetVersion.EXCEL2007).formatAsString()
                 );

    CTNumDataSource ctNumDataSource = ctLineSer.getVal();
    CTNumRef ctNumRef = ctNumDataSource.getNumRef();

    AreaReference numReference = new AreaReference(ctNumRef.getF(), SpreadsheetVersion.EXCEL2007);
    CellReference firstNumCell = numReference.getFirstCell();
    CellReference lastNumCell = numReference.getLastCell();
    if (lastNumCell.getRow() == lastRow && firstNumCell.getCol() == lastNumCell.getCol()) {
     col = firstNumCell.getCol();
     row = sheet.getRow(lastRow+1); if (row == null) row = sheet.createRow(lastRow+1);
     cell = row.getCell(col); if (cell == null) cell = row.createCell(col);
     if (ser < seriesData.length) cell.setCellValue(seriesData[ser]);

     ctNumRef.setF(new AreaReference(
                   firstNumCell, 
                   new CellReference(lastNumCell.getSheetName(), lastRow+1, col, true, true), 
                   SpreadsheetVersion.EXCEL2007).formatAsString()
                  );
    }
   }
   ser++;
  }
 }

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

  XSSFWorkbook workbook = (XSSFWorkbook)WorkbookFactory.create(new FileInputStream("WorkbookWithChart.xlsx"));

  XSSFSheet sheet = workbook.getSheetAt(0);

  XSSFChart chart = getChartWithTitle(sheet, "Z-Acceleration"); 

  if (chart != null) {
   addMonthDataToChart(sheet, chart, "Apr", new Double[]{7d,3d,5d});
   addMonthDataToChart(sheet, chart, "Mai", new Double[]{2d,6d,8d});
   addMonthDataToChart(sheet, chart, "Jun", new Double[]{1d,9d,4d});
   addMonthDataToChart(sheet, chart, "Jul", new Double[]{5d,6d});
  }

  FileOutputStream out = new FileOutputStream("WorkbookWithChartNew.xlsx");
  workbook.write(out);
  out.close();
  workbook.close();
 }
}

通过将行高设置为零,可以从图表显示中删除数据,而不是添加新行

首先,创建具有最大可能数据范围的图表,如下所示。

然后,使用以下代码设置不希望在高度为零的图形中看到的行

    for(int i=8;i<14;i++) {
        sheet.getRow(i).setZeroHeight(true);;
    }

for(int i=8;i您拥有
org.openxmlformats.schemas.drawingml.x2006.chart.CTLineSer
,因此您当然可以更改它。但是您需要知道更改什么(XML
的含义是什么)以及如何更改(下载javadoc的源代码并从中执行
javadoc
,以获得
API
文档)但是为什么不使用ApachePOI4.1.0的新的
XDDF
而不是那样尝试呢?@AxelRichter我意识到我仍然在使用3.17,并且刚刚做了更改。但是,我已经尝试下载ooxml模式的源代码,并且只有编译的类文件,没有javadocs。从下载
ooxml-schemas-1.3-sources.jar
。将其解压缩。然后进入目录
ooxml-schemas-1.3
并执行
javadoc-d javadoc-sourcepath./-subpackages org
。然后在
ooxml-schemas-1.3/javadoc
中找到
API
文档。从
overview tree.html
开始。这是我见过的最详细、最有用的答案之一在这里几年后发现的谢谢Axel。
    for(int i=8;i<14;i++) {
        sheet.getRow(i).setZeroHeight(true);;
    }