/ / Apache POI générer un tableau Excel - java, excel, apache-poi

Apache POI générer un tableau Excel - java, excel, apache-poi

Tout:

Très nouveau pour Apache POI et excel VBA, comment puis-je implémenter quelque chose comme ceci en Java avec Apache POI ou tout autre librairie java:

Code VBA dans Excel:

Sheets("Sheet 1").ChartObjects("Line Chart")
.Chart.Axes(xlValue).MaximumScale = Sheets("Sheet 1").Range("A37")

Je ne trouve aucune API associée à un graphique qui fonctionne de la sorte.

Vous pouvez également utiliser Java pour mettre à jour automatiquement la cellule A37 et lui attribuer la valeur "Echelle xAxis Max du" Graphique en courbes ". Existe-t-il un moyen d’appeler directement ce code VBA via un POI?

Merci,

Réponses:

1 pour la réponse № 1

Réponse pour la dernière version stable actuelle apache poi 3.17. Remarque XSSFChart est en développement. Donc, nous devrions utiliser XDDFChart pour les versions ultérieures.

Vous pouvez obtenir un List de XSSFCharts depuis le dessin de la feuille via XSSFDrawing.getCharts. À partir de ce List obtenir votre nécessaire XSSFChart. Ensuite, obtenez les axes de ce graphique via XSSFChart.getAxis. Puis obtenir le approprié XSSFValueAxis à partir de ce List. Et ensuite, changez-le au maximum via XSSFChartAxis.setMaximum.

Exemple:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xssf.usermodel.charts.*;

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

class ReadAndWriteExcelXSSFChart {

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

Workbook workbook = WorkbookFactory.create(new FileInputStream("WBWithLineChart.xlsm"));
Sheet sheet = workbook.getSheetAt(0);

Row row = sheet.getRow(36); if (row == null) row = sheet.createRow(36);
Cell cell = row.getCell(0); if (cell == null) cell = row.createCell(0);

cell.setCellValue(10);

double valueA37 = cell.getNumericCellValue();

Drawing drawing = sheet.getDrawingPatriarch();
if (drawing instanceof XSSFDrawing) {
for (XSSFChart chart : ((XSSFDrawing)drawing).getCharts()) {
System.out.println(chart.getPackagePart().getPartName().getName());
if (chart.getPackagePart().getPartName().getName().endsWith("chart1.xml")) { //first chart in sheet
for (XSSFChartAxis axis : chart.getAxis()) { //all axes
System.out.println(axis);
if (axis instanceof XSSFValueAxis) { //value axis
axis.setMaximum(valueA37); // maximum = same value as in A37
System.out.println(axis.getMaximum());
}
}
}
}
}

workbook.write(new FileOutputStream("WBWithLineChart.xlsm"));
workbook.close();

}
}