用poi3和chartdirect生成带图表的excel报表
2009-06-04 16:10
323 查看
源码如下:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.*;
import org.apache.poi.hssf.model.PictureShape;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import ChartDirector.Chart;
import ChartDirector.ChartViewer;
import ChartDirector.LineLayer;
import ChartDirector.XYChart;
public class TestPOI
{
public static void main(String[] args)
{
FileOutputStream fileOut = null;
try
{
// 创建一个图片
double[] data0 = { 42, 49, 33, 38, 51, 46, 29, 41, 44, 57, 59, 52,
37, 34, 51, 56, 56, 60, 70, 76, 63, 67, 75, 64, 51 };
double[] data1 = { 50, 55, 47, 34, 42, 49, 63, 62, 73, 59, 56, 50,
64, 60, 67, 67, 58, 59, 73, 77, 84, 82, 80, 84, 98 };
double[] data2 = { 36, 28, 25, 33, 38, 20, 22, 30, 25, 33, 30, 24,
28, 15, 21, 26, 46, 42, 48, 45, 43, 52, 64, 60, 70 };
String[] labels = { "0", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24" };
XYChart c = new XYChart(500, 300, 0xffff80, 0x0, 1);
c.setPlotArea(55, 45, 420, 210, 0xffffff, -1, -1, 0xc0c0c0, -1);
c.addLegend(55, 25, false, "", 8).setBackground(Chart.Transparent);
c.addTitle("都是活雷锋", "宋体 Bold Italic", 11, 0xffffff)
.setBackground(0x800000, -1, 1);
c.yAxis().setTitle("mm");
c.xAxis().setLabels(labels);
c.xAxis().setLabelStep(3);
c.xAxis().setTitle("Jun 12, 2001");// Add a line layer to the chart
LineLayer layer = c.addLineLayer2();
layer.setLineWidth(2);
layer.addDataSet(data0, -1, "Server # 1");
layer.addDataSet(data1, -1, "Server # 2");
layer.addDataSet(data2, c.dashLineColor(0x3333ff, Chart.DashLine),
"Server # 3");
FileInputStream origin = null;
HSSFWorkbook wb = null;
POIFSFileSystem fsorigin = null;
try
{
origin = new FileInputStream("C://PRfcas//table.xls");
}
catch (java.io.FileNotFoundException e)
{
System.out.print(e);
}
try
{
fsorigin = new POIFSFileSystem(origin);
wb = new HSSFWorkbook(fsorigin);
}
catch (Exception e)
{
System.out.print(e);
}
HSSFFont f1 = wb.createFont();
f1.setFontHeightInPoints((short) 10);
f1.setFontName("宋体");
HSSFFont f2 = wb.createFont();
f2.setFontHeightInPoints((short) 12);
f2.setFontName("宋体");
// 指定sheet
HSSFSheet sheet1 = wb.getSheetAt(0);
sheet1.setDefaultColumnWidth((short) 10);
// 储存任务单项的名称格式
HSSFCellStyle c1 = sheet1.getRow(0).getCell((short) 0)
.getCellStyle();
List<String> columnname = new ArrayList<String>();
columnname.add("第一列");
columnname.add("第二列");
columnname.add("第三列");
List<List<String>> content = new ArrayList<List<String>>();
List<String> temp = new ArrayList();
temp.add("sd");
temp.add("sd");
temp.add("sd");
content.add(temp);
content.add(temp);
content.add(temp);
HSSFCell ce = null;
HSSFRow r = null;
int rowcount = 0;
int rowset = 0;
if (columnname.size() != 0)
{
r = sheet1.createRow(0);
for (int i = 0; i < columnname.size(); i++)
{
ce = r.createCell((short) i);
ce.setCellStyle(c1);
ce.setEncoding((short) 1);
ce.setCellValue((String) columnname.get(i));
}
rowcount++;
int n = 0;
// 写入第一个表的数据内容
for (n = 0; n < content.size(); n++)
{
r = sheet1.createRow(rowcount);
ArrayList value = (ArrayList) content.get(n);
for (int m = 0; m <= value.size() - 1; m++)
{
ce = r.createCell((short) m);
ce.setCellStyle(c1);
ce.setEncoding((short) 1);
if (value.get(m) != null)
{
String tmpstr = (String) value.get(m);
// if (tmpstr.indexOf(",")!=-1)
// tmpstr = tmpstr.substring(0,tmpstr.indexOf(","));
ce.setCellValue(tmpstr);
}
else
ce.setCellValue("空");
}
rowcount++;
}
}
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,
(short) 0, 10, (short) 7, 30);
// 插入图片
patriarch.createPicture(anchor, wb.addPicture(c.makeChart2(2),
HSSFWorkbook.PICTURE_TYPE_JPEG));
fileOut = new FileOutputStream("d:/workbook.xls");
// 写入excel文件
wb.write(fileOut);
fileOut.close();
}
catch (IOException io)
{
io.printStackTrace();
System.out.println("io erorr : " + io.getMessage());
}
finally
{
if (fileOut != null)
{
try
{
fileOut.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}
上面的代码单独运行的时候是没有任何问题的,但是当我把主要的代码放到weblogic中运行的时候页面出现了request url null异常,检查了无数遍没有发现代码错误,经调试,发现代码执行到这句的时候中断了 HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
让我百思不得其解的是同样是这句代码,为啥单独执行的时候没有任何问题,一放到weblogic下就出问题了呢???看来问题出在应用引入的jar包上面,看了一下,发现chartdirect的引用库下面还多出几个同事加的莫名奇妙的jar包,遂删除之,再运行,问题得到解决。估计是这几个jar包和weblogic自带的冲突了。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.*;
import org.apache.poi.hssf.model.PictureShape;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import ChartDirector.Chart;
import ChartDirector.ChartViewer;
import ChartDirector.LineLayer;
import ChartDirector.XYChart;
public class TestPOI
{
public static void main(String[] args)
{
FileOutputStream fileOut = null;
try
{
// 创建一个图片
double[] data0 = { 42, 49, 33, 38, 51, 46, 29, 41, 44, 57, 59, 52,
37, 34, 51, 56, 56, 60, 70, 76, 63, 67, 75, 64, 51 };
double[] data1 = { 50, 55, 47, 34, 42, 49, 63, 62, 73, 59, 56, 50,
64, 60, 67, 67, 58, 59, 73, 77, 84, 82, 80, 84, 98 };
double[] data2 = { 36, 28, 25, 33, 38, 20, 22, 30, 25, 33, 30, 24,
28, 15, 21, 26, 46, 42, 48, 45, 43, 52, 64, 60, 70 };
String[] labels = { "0", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24" };
XYChart c = new XYChart(500, 300, 0xffff80, 0x0, 1);
c.setPlotArea(55, 45, 420, 210, 0xffffff, -1, -1, 0xc0c0c0, -1);
c.addLegend(55, 25, false, "", 8).setBackground(Chart.Transparent);
c.addTitle("都是活雷锋", "宋体 Bold Italic", 11, 0xffffff)
.setBackground(0x800000, -1, 1);
c.yAxis().setTitle("mm");
c.xAxis().setLabels(labels);
c.xAxis().setLabelStep(3);
c.xAxis().setTitle("Jun 12, 2001");// Add a line layer to the chart
LineLayer layer = c.addLineLayer2();
layer.setLineWidth(2);
layer.addDataSet(data0, -1, "Server # 1");
layer.addDataSet(data1, -1, "Server # 2");
layer.addDataSet(data2, c.dashLineColor(0x3333ff, Chart.DashLine),
"Server # 3");
FileInputStream origin = null;
HSSFWorkbook wb = null;
POIFSFileSystem fsorigin = null;
try
{
origin = new FileInputStream("C://PRfcas//table.xls");
}
catch (java.io.FileNotFoundException e)
{
System.out.print(e);
}
try
{
fsorigin = new POIFSFileSystem(origin);
wb = new HSSFWorkbook(fsorigin);
}
catch (Exception e)
{
System.out.print(e);
}
HSSFFont f1 = wb.createFont();
f1.setFontHeightInPoints((short) 10);
f1.setFontName("宋体");
HSSFFont f2 = wb.createFont();
f2.setFontHeightInPoints((short) 12);
f2.setFontName("宋体");
// 指定sheet
HSSFSheet sheet1 = wb.getSheetAt(0);
sheet1.setDefaultColumnWidth((short) 10);
// 储存任务单项的名称格式
HSSFCellStyle c1 = sheet1.getRow(0).getCell((short) 0)
.getCellStyle();
List<String> columnname = new ArrayList<String>();
columnname.add("第一列");
columnname.add("第二列");
columnname.add("第三列");
List<List<String>> content = new ArrayList<List<String>>();
List<String> temp = new ArrayList();
temp.add("sd");
temp.add("sd");
temp.add("sd");
content.add(temp);
content.add(temp);
content.add(temp);
HSSFCell ce = null;
HSSFRow r = null;
int rowcount = 0;
int rowset = 0;
if (columnname.size() != 0)
{
r = sheet1.createRow(0);
for (int i = 0; i < columnname.size(); i++)
{
ce = r.createCell((short) i);
ce.setCellStyle(c1);
ce.setEncoding((short) 1);
ce.setCellValue((String) columnname.get(i));
}
rowcount++;
int n = 0;
// 写入第一个表的数据内容
for (n = 0; n < content.size(); n++)
{
r = sheet1.createRow(rowcount);
ArrayList value = (ArrayList) content.get(n);
for (int m = 0; m <= value.size() - 1; m++)
{
ce = r.createCell((short) m);
ce.setCellStyle(c1);
ce.setEncoding((short) 1);
if (value.get(m) != null)
{
String tmpstr = (String) value.get(m);
// if (tmpstr.indexOf(",")!=-1)
// tmpstr = tmpstr.substring(0,tmpstr.indexOf(","));
ce.setCellValue(tmpstr);
}
else
ce.setCellValue("空");
}
rowcount++;
}
}
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,
(short) 0, 10, (short) 7, 30);
// 插入图片
patriarch.createPicture(anchor, wb.addPicture(c.makeChart2(2),
HSSFWorkbook.PICTURE_TYPE_JPEG));
fileOut = new FileOutputStream("d:/workbook.xls");
// 写入excel文件
wb.write(fileOut);
fileOut.close();
}
catch (IOException io)
{
io.printStackTrace();
System.out.println("io erorr : " + io.getMessage());
}
finally
{
if (fileOut != null)
{
try
{
fileOut.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}
上面的代码单独运行的时候是没有任何问题的,但是当我把主要的代码放到weblogic中运行的时候页面出现了request url null异常,检查了无数遍没有发现代码错误,经调试,发现代码执行到这句的时候中断了 HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
让我百思不得其解的是同样是这句代码,为啥单独执行的时候没有任何问题,一放到weblogic下就出问题了呢???看来问题出在应用引入的jar包上面,看了一下,发现chartdirect的引用库下面还多出几个同事加的莫名奇妙的jar包,遂删除之,再运行,问题得到解决。估计是这几个jar包和weblogic自带的冲突了。
相关文章推荐
- 在.NET中不安装Office使用EPPlus生成带图表(Chart)的Excel报表
- 在.NET中不安装Office使用EPPlus生成带图表(Chart)的Excel报表
- 在.NET中不安装Office使用EPPlus生成带图表(Chart)的Excel报表
- Excel生成报表之解决方案---合并图表
- 生成Excel报表的最直观方法
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)4
- Python 生成CTS Excel 报表
- oracle自动生成excel报表(时间为变量,条件含变量)
- 使用Aspose.Cell控件实现Excel高难度报表的生成(一)
- python3查询数据库并生成excel报表
- 从DataView中生成Excel报表的方案
- 利用 LotusScript 实现用 Lotus Domino 的数据生成图形化的 Excel 报表
- poi jxl 生成EXCEL 报表
- 11、借助POI实现Java生成并打印excel报表(2)
- Javascript 实现 Excel 导入生成图表功能
- [收藏].net关于企业Excel报表的生成
- 导入导出封装工具类(二) jXLS Excel报表生成工具类
- JAVA操作Excel 可配置,动态 生成复杂表头 复杂的中国式报表表头
- 从DataView中生成Excel报表的方案(C#) - 清清月儿 .NET万花筒 Asp.net技术 Asp.net教程 Asp.net源码 Asp.net基础 Asp.net控件 Asp.net入门 - CSDNBlog
- 数据库内容导出到EXCEL生成报表和图形的方法