为什么XSSFWorksheet的getName()不起作用?

时间:2015-03-09 08:15:31

标签: java excel apache-poi jfreechart

我正在尝试使用poi lib创建我的xlsx文件的图表。但每当我尝试使用getName()获取工作表时,它就无法工作:它返回null。我已经检查了很多时间,我的sample.xlsx文件有工作表Date和Sales。但我不是试图获取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文件中?

1 个答案:

答案 0 :(得分:0)

我不知道为什么没有像预期的那样工作,但回答你用java制作图表的问题我能想到的唯一工作周期是坚持数值范围。

在这里,您可以看到apache poi(source)的线图示例:

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

}