How to manipulate Excel files with Apache POI-----How to draw a discrete graph with Apache POI

Source: Internet
Author: User

Sometimes, we need the data in Excel, through a picture, visualize the display. So what should be done at this time? Now Jiehuaxianfo, take an example from Apache poi to give you a demonstration of how the POI API is drawing. The following is a final. Then we will explain the function and meaning of each piece of code separately.

Code as follows,

[Java]View PlainCopy 
  1. Import Java.io.FileOutputStream;
  2. Import org.apache.poi.ss.usermodel.*;
  3. Import org.apache.poi.ss.util.*;
  4. Import org.apache.poi.ss.usermodel.charts.*;
  5. Import Org.apache.poi.xssf.usermodel.XSSFWorkbook;
  6. /**
  7. * Illustrates how to create a simple scatter chart.
  8. *
  9. * @author Roman Kashitsyn
  10. */
  11. Public class Scatterchart {
  12. public static void Main (string[] args) throws Exception {
  13. Workbook WB = new Xssfworkbook ();
  14. Sheet Sheet = Wb.createsheet ("Sheet 1");
  15. final int num_of_rows = 3;
  16. final int num_of_columns = 10;
  17. //Create a row and put some cells in it.  Rows are 0 based.
  18. Row row;
  19. Cell cell;
  20. For (int rowIndex = 0; rowIndex < num_of_rows; rowindex++) {
  21. row = Sheet.createrow ((short) rowIndex);
  22. For (int colindex = 0; colindex < num_of_columns; colindex++) {
  23. Cell = Row.createcell ((short) colindex);
  24. Cell.setcellvalue (Colindex * (RowIndex + 1));
  25. }
  26. }
  27. Drawing Drawing = Sheet.createdrawingpatriarch ();
  28. Clientanchor anchor = Drawing.createanchor (0, 0, 0, 0, 0, 5, Ten, 15);
  29. Chart chart = Drawing.createchart (anchor);
  30. ChartLegend legend = Chart.getorcreatelegend ();
  31. Legend.setposition (Legendposition.top_right);
  32. Scatterchartdata data = Chart.getchartdatafactory (). Createscatterchartdata ();
  33. Valueaxis Bottomaxis = Chart.getchartaxisfactory (). Createvalueaxis (Axisposition.bottom);
  34. Valueaxis Leftaxis = Chart.getchartaxisfactory (). Createvalueaxis (Axisposition.left);
  35. Leftaxis.setcrosses (Axiscrosses.auto_zero);
  36. chartdatasource<number> xs = datasources.fromnumericcellrange (sheet, new cellrangeaddress (0, 0,  0, Num_of_columns- 1));
  37. chartdatasource<number> ys1 = datasources.fromnumericcellrange (sheet, new cellrangeaddress (1, 1,  0, Num_of_columns- 1));
  38. chartdatasource<number> ys2 = datasources.fromnumericcellrange (sheet, new Cellrangeaddress (2, 2,  0, Num_of_columns- 1));
  39. Data.addserie (xs, ys1);
  40. Data.addserie (xs, YS2);
  41. Chart.plot (data, Bottomaxis, Leftaxis);
  42. //Write the output to a file
  43. FileOutputStream fileout = new FileOutputStream ("ooxml-scatter-chart.xlsx");
  44. Wb.write (fileout);
  45. Fileout.close ();
  46. }
  47. }

The following decomposition:

1. The following code creates a new workbook and a work form object

[Java]View PlainCopy 
    1. Workbook WB = new Xssfworkbook ();
    2. Sheet Sheet = Wb.createsheet ("Sheet 1");


2. The following code is generated to initialize the data, the total data has 3 rows 10 columns.

[Java]View PlainCopy  
  1. Final int num_of_rows = 3;
  2. Final int num_of_columns = 10;
  3. //Create a row and put some cells in it.  Rows are 0 based.
  4. Row row;
  5. Cell cell;
  6. For (int rowIndex = 0; rowIndex < num_of_rows; rowindex++) {
  7. row = Sheet.createrow ((short) rowIndex);
  8. For (int colindex = 0; colindex < num_of_columns; colindex++) {
  9. Cell = Row.createcell ((short) colindex);
  10. Cell.setcellvalue (Colindex * (RowIndex + 1));
  11. }
  12. }


3. The following code sets the area of paint: Starting at line 5th, ending with 15 lines, occupying 10 columns in total

[Java]View PlainCopy 
    1. Drawing Drawing = Sheet.createdrawingpatriarch ();
    2. Clientanchor anchor = Drawing.createanchor (0, 0, 0, 0, 0, 5, Ten, 15);

4. Create a coordinate system for a discrete graph

[Java]View PlainCopy 
    1. Chart chart = Drawing.createchart (anchor);
    2. ChartLegend legend = Chart.getorcreatelegend ();
    3. Legend.setposition (Legendposition.top_right);
    4. Scatterchartdata data = Chart.getchartdatafactory (). Createscatterchartdata ();
    5. Valueaxis Bottomaxis = Chart.getchartaxisfactory (). Createvalueaxis (Axisposition.bottom);
    6. Valueaxis Leftaxis = Chart.getchartaxisfactory (). Createvalueaxis (Axisposition.left);
    7. Leftaxis.setcrosses (Axiscrosses.auto_zero);

5. Populating data on discrete graphs

[Java]View PlainCopy 
  1. chartdatasource<number> xs = datasources.fromnumericcellrange (sheet, new cellrangeaddress (0, 0,  0, Num_of_columns- 1));
  2. chartdatasource<number> ys1 = datasources.fromnumericcellrange (sheet, new cellrangeaddress (1, 1,  0, Num_of_columns- 1));
  3. chartdatasource<number> ys2 = datasources.fromnumericcellrange (sheet, new Cellrangeaddress (2, 2,  0, Num_of_columns- 1));
  4. Data.addserie (xs, ys1);
  5. Data.addserie (xs, YS2);

wherein, the following method defines

[Java]View PlainCopy 
    1. Datasources.fromnumericcellrange (sheet, new cellrangeaddress (0, 0, 0, Num_of_columns- 1));

As follows

[Java]View PlainCopy  
    1. Public static chartdatasource<number> Fromnumericcellrange (Sheet Sheet, cellrangeaddress cellrangeaddress)

As can be seen from the above, the key method of filling the data is that

[Java]View PlainCopy 
    1. <pre name="code" class="java" >new Cellrangeaddress (0, 0, 0, Num_of_columns- 1) /c8>

So how does this approach be defined?

[Java]View PlainCopy 
    1. Public cellrangeaddress (int firstrow, int lastrow, int firstcol, int lastcol)

As can be seen from the above, it allows us to set the data from that line, the end of the line, that column began, that column is over.

In the above code 5 sentences, respectively, the 1th row of 1 to 10 columns as the benchmark, and then the 2nd row of 1 to 10 to make a comparison, draw the Curve series 1

Make a comparison of rows 1 to 10 of line 1th, then compare the 1 to 10 columns of line 3rd, and draw the Curve series 2

6. Start Drawing

[Java]View PlainCopy  
    1. Chart.plot (data, Bottomaxis, Leftaxis);


7. Save as an Excel file

[Java]View PlainCopy 
      1. FileOutputStream fileout = new FileOutputStream ("ooxml-scatter-chart.xlsx");
      2. Wb.write (fileout);
      3. Fileout.close ();

How to manipulate Excel files with Apache POI-----How to draw a discrete graph with Apache POI

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.