java为什么XSSFWorksheet的getName()不起作用?
我正在尝试使用poi lib创建xlsx文件的图表。但每当我尝试使用getName()
获取工作表时,它就不起作用:它返回null
。我已经检查了很多时间我的样品。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());
}
问题-如何简单地使用java将所需图表创建到excel文件中
共 (0) 个答案