使用poi操作excel_画图
2017-06-14 09:29
288 查看
package com.fujitsu.fb.reporter.subprocess;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.ss.usermodel.charts.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* Illustrates how to create a simple scatter chart.
*
* @author Roman Kashitsyn
*/
public class ScatterChart
{
public static void main(String[] args) throws Exception
{
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet 1");
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, 2, 5, 10, 15);//设置坐标
Chart chart = drawing.createChart(anchor);//创建图表
ChartLegend legend = chart.getOrCreateLegend();//获取或创建图例
legend.setPosition(LegendPosition.TOP_RIGHT);//设置图例位置
ScatterChartData data = chart.getChartDataFactory().createScatterChartData();//获取分散数据
ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(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.addSerie(xs, ys1);
data.addSerie(xs, ys2);
chart.plot(data, bottomAxis, leftAxis);
String excelPath = "E:\\poi\\ooxml-scatter-chart.xlsx";
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream(excelPath);
wb.write(fileOut);
fileOut.close();
System.out.println("----Excle文件已生成------");
}
}
package com.fujitsu.fb.reporter.subprocess;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.ss.usermodel.charts.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* Illustrates how to create a simple scatter chart.
*
* @author Roman Kashitsyn
*/
public class ScatterChart
{
public static void main(String[] args) throws Exception
{
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet 1");
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, 2, 5, 10, 15);//设置坐标
Chart chart = drawing.createChart(anchor);//创建图表
ChartLegend legend = chart.getOrCreateLegend();//获取或创建图例
legend.setPosition(LegendPosition.TOP_RIGHT);//设置图例位置
ScatterChartData data = chart.getChartDataFactory().createScatterChartData();//获取分散数据
ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(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.addSerie(xs, ys1);
data.addSerie(xs, ys2);
chart.plot(data, bottomAxis, leftAxis);
String excelPath = "E:\\poi\\ooxml-scatter-chart.xlsx";
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream(excelPath);
wb.write(fileOut);
fileOut.close();
System.out.println("----Excle文件已生成------");
}
}
相关文章推荐
- 在JSP中如何操作Excel----POI插件的使用
- 使用poi操作Excel的几点注意事项
- java使用poi操作excel(读、写)
- 学习使用poi.jar操作Excel
- 使用poi操作excel
- 使用POI操作Excel
- Java POI 操作Excel总结笔记--使用配置和使用案例篇
- 使用POI操作excel(读取和创建)快速开发指南
- 使用POI操作excel(读取和创建)快速开发指南
- 转:使用poi操作Excel的几点注意事项
- 使用POI包进行Excel文件操作(2)
- 屁孩博客:使用POI操作Excel:Sheet拷贝
- 使用POI包进行Excel文件操作(1)
- 使用poi操作导入excel代码示例
- 使用poi操作Excel的几点注意事项
- 在Struts+Spring+ibaits中使用poi进行excel操作时候的问题
- Java POI 操作Excel总结笔记--使用配置和使用案例篇
- 使用poi技术操作Excel(一)
- 使用POI操作Excel和Word
- 使用poi操作导出excel代码示例