Java 为什么XSSFWorksheet的getName()不起作用?
我正在尝试使用poi lib创建xlsx文件的图表。但每当我尝试使用Java 为什么XSSFWorksheet的getName()不起作用?,java,excel,apache-poi,jfreechart,Java,Excel,Apache Poi,Jfreechart,我正在尝试使用poi lib创建xlsx文件的图表。但每当我尝试使用getName()获取工作表时,它都不起作用:它返回null。我已经检查了很多时间我的sample.xlsx文件有工作表日期和销售。但我不会尝试获取XSSFName对象 private static void generateExcelChart() throws IOException, InvalidFormatException { int deface=1; int rowNum=7; //Load sample ex
getName()
获取工作表时,它都不起作用:它返回null
。我已经检查了很多时间我的sample.xlsx文件有工作表日期和销售。但我不会尝试获取XSSFName对象
private static void generateExcelChart() throws IOException, InvalidFormatException {
int deface=1;
int rowNum=7;
//Load sample excel file
FileInputStream is = new FileInputStream(new File("sample.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(is); // or sample.xlsx
CreationHelper createHelper = workbook.getCreationHelper();
Sheet sh=workbook.getSheetAt(0);
String sheetName=sh.getSheetName();
//create cell style for date format
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(
createHelper.createDataFormat().getFormat("d/m/yyyy"));
//Clear dummy values
sh.getRow(1).getCell(0).setCellValue("");
sh.getRow(1).getCell(1).setCellValue("");
//Set headers for the data
sh.createRow(0).createCell(2).setCellValue("Date");
sh.getRow(0).createCell(3).setCellValue("Sales");
Cell datecell = null;
Cell salescell = null;
// Populate C2 to C8 and D2 to D8 with chart data
for(int i=1;i<=7;i++){
Row r=sh.getRow(i);
if(r==null)
r=sh.createRow(i);
datecell=r.getCell(2);
salescell=r.getCell(3);
switch(i){
case 1:
if(datecell==null){
datecell=r.createCell(2);
datecell.setCellValue("1/1/2012");
datecell.setCellStyle(cellStyle);
}
else{
datecell.setCellValue("1/1/2012");
datecell.setCellStyle(cellStyle);
}
if(salescell==null)
r.createCell(3).setCellValue(2000);
else
salescell.setCellValue(2000);
break;
case 2:
if(datecell==null){
datecell=r.createCell(2);
datecell.setCellValue("1/2/2012");
datecell.setCellStyle(cellStyle);
}
else{
datecell.setCellValue("1/2/2012");
datecell.setCellStyle(cellStyle);
}
if(salescell==null)
r.createCell(3).setCellValue(1000);
else
salescell.setCellValue(1000);
break;
case 3:
if(datecell==null){
datecell=r.createCell(2);
datecell.setCellValue("1/3/2012");
datecell.setCellStyle(cellStyle);
}
else{
datecell.setCellValue("1/3/2012");
datecell.setCellStyle(cellStyle);
}
if(salescell==null)
r.createCell(3).setCellValue(4000);
else
salescell.setCellValue(4000);
break;
case 4:
if(datecell==null){
datecell=r.createCell(2);
datecell.setCellValue("1/4/2012");
datecell.setCellStyle(cellStyle);
}
else{
datecell.setCellValue("1/4/2012");
datecell.setCellStyle(cellStyle);
}
if(salescell==null)
r.createCell(3).setCellValue(2500);
else
salescell.setCellValue(2500);
break;
case 5:
if(datecell==null){
datecell=r.createCell(2);
datecell.setCellValue("1/5/2012");
datecell.setCellStyle(cellStyle);
}
else{
datecell.setCellValue("1/5/2012");
datecell.setCellStyle(cellStyle);
}
if(salescell==null)
r.createCell(3).setCellValue(3000);
else
salescell.setCellValue(3000);
break;
case 6:
if(datecell==null){
datecell=r.createCell(2);
datecell.setCellValue("1/6/2012");
datecell.setCellStyle(cellStyle);
}
else{
datecell.setCellValue("1/6/2012");
datecell.setCellStyle(cellStyle);
}
if(salescell==null)
r.createCell(3).setCellValue(4000);
else
salescell.setCellValue(4000);
break;
case 7:
if(datecell==null){
datecell=r.createCell(2);
datecell.setCellStyle(cellStyle);
datecell.setCellValue("1/8/2012");
}
else{
datecell.setCellValue("1/8/2012");
datecell.setCellStyle(cellStyle);
}
if(salescell==null)
r.createCell(3).setCellValue(5000);
else
salescell.setCellValue(5000);
break;
default:
System.out.println("Invalid Input");
break;
}
}
System.out.println(workbook);
//Search for named range
XSSFName rangeCell = workbook.getName("Date");
//Set new range for named range
String reference = sheetName + "!$C$" + ( deface+1 ) + ":$C$" + (rowNum+deface);
//Assigns range value to named range
rangeCell.setRefersToFormula(reference);
rangeCell = workbook.getName("Sales");
reference = sheetName + "!$D$"+(deface+1) + ":$D$" + (rowNum+deface);
rangeCell.setRefersToFormula(reference);
FileOutputStream f = new FileOutputStream("sample-out.xlsx");
workbook.write(f);
f.close();
System.out.println("Number Of Sheets" + workbook.getNumberOfSheets());
Sheet s = workbook.getSheetAt(0);
System.out.println("Number Of Rows:" + s.getLastRowNum());
private static void generateExcelChart()引发IOException,InvalidFormatException{
int-deface=1;
int rowNum=7;
//加载示例excel文件
FileInputStream是=新的FileInputStream(新文件(“sample.xlsx”);
XSSF工作簿=新XSSF工作簿(is);//或sample.xlsx
CreationHelper createHelper=workbook.getCreationHelper();
Sheet sh=工作簿。getSheetAt(0);
String sheetName=sh.getSheetName();
//为日期格式创建单元格样式
CellStyle CellStyle=workbook.createCellStyle();
cellStyle.setDataFormat(
createHelper.createDataFormat().getFormat(“d/m/yyyy”);
//清除虚拟值
sh.getRow(1).getCell(0).setCellValue(“”);
sh.getRow(1).getCell(1).setCellValue(“”);
//设置数据的标题
sh.createRow(0).createCell(2).setCellValue(“日期”);
sh.getRow(0).createCell(3).setCellValue(“销售”);
Cell-datecell=null;
Cell-salescell=null;
//用图表数据填充C2至C8和D2至D8
对于(inti=1;i我不知道为什么不能按预期工作,但回答您关于用java制作图表的问题,我能想到的唯一方法就是坚持数值范围
在这里可以看到ApachePOI()的线形图示例:
公共类线形图{
公共静态void main(字符串[]args)引发异常{
工作簿wb=新XSSFWorkbook();
图纸=wb.createSheet(“线形图”);
行的最终整数=3;
_列的最终int NUM_=10;
//创建一行并在其中放置一些单元格。行基于0。
行行;
细胞;
for(int-rowIndex=0;rowIndex
}
public class LineChart {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("linechart");
final int NUM_OF_ROWS = 3;
final int NUM_OF_COLUMNS = 10;
// Create a row and put some cells in it. Rows are 0 based.
Row row;
Cell cell;
for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
row = sheet.createRow((short) rowIndex);
for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
cell = row.createCell((short) colIndex);
cell.setCellValue(colIndex * (rowIndex + 1));
}
}
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
Chart chart = drawing.createChart(anchor);
ChartLegend legend = chart.getOrCreateLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
LineChartData data = chart.getChartDataFactory().createLineChartData();
// Use a category axis for the bottom axis.
ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
data.addSeries(xs, ys1);
data.addSeries(xs, ys2);
chart.plot(data, bottomAxis, leftAxis);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx");
wb.write(fileOut);
fileOut.close();
}