首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

用Java怎么生成Excel工作薄和Chart

2012-10-29 
用Java如何生成Excel工作薄和Chart请帮帮忙,谢谢import java.io.FileOutputStreamimport java.io.OutputS

用Java如何生成Excel工作薄和Chart
请帮帮忙,谢谢import java.io.FileOutputStream;import java.io.OutputStream;import jxl.Workbook;import jxl.format.Alignment;import jxl.format.Border;import jxl.format.BorderLineStyle;import jxl.format.Colour;import jxl.format.VerticalAlignment;import jxl.write.Label;import jxl.write.WritableCellFormat;import jxl.write.WritableFont;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;public class MyExcel {/** * @param args */public static void main(String[] args) {String targetfile = "d:/out.xls";// 输出的excel文件名String worksheet = "List";// 输出的excel文件工作表名String[] title = { "ID", "NAME", "DESCRIB" };// excel工作表的标题WritableWorkbook workbook;try {System.out.println("begin");OutputStream os = new FileOutputStream(targetfile);workbook = Workbook.createWorkbook(os);/*自定义颜色*/workbook.setColourRGB(Colour.GREEN, 128, 128, 128); workbook.setColourRGB(Colour.BLUE, 128, 0, 128); //workbook.setColourRGB(Colour.BLUE2, 198, 239, 206); //背景颜色workbook.setColourRGB(Colour.BLUE2, 204, 255, 255); //背景颜色WritableSheet sheet = workbook.createSheet(worksheet, 0); // 添加第一个工作表// 设置标题格式 WritableFont wfTitle = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK); //true表示设置斜体 WritableCellFormat wcfTitle = new WritableCellFormat(wfTitle); wcfTitle.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLUE);wcfTitle.setBackground(Colour.GREEN);// 设置水平对齐方式 wcfTitle.setAlignment(Alignment.CENTRE); // 设置垂直对齐方式 wcfTitle.setVerticalAlignment(VerticalAlignment.CENTRE); // 设置是否自动换行 wcfTitle.setWrap(true); // 合并A1->C2 mergeCells(起始列, 起始行, 终止列, 合并行数-1)sheet.mergeCells(0, 0, 2, 1); Label titleCell = new Label(0, 0, "Title Cell ", wcfTitle); sheet.addCell(titleCell);// 创建可写入的Excel工作薄,运行生成的文件在tomcat/bin下// workbook = Workbook.createWorkbook(new File("output.xls"));//设置内容格式WritableCellFormat contentFormat = new WritableCellFormat();contentFormat.setAlignment(Alignment.CENTRE);//contentFormat.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLUE);contentFormat.setBorder(Border.LEFT,BorderLineStyle.DASHED,Colour.BLUE);contentFormat.setBorder(Border.TOP,BorderLineStyle.THIN,Colour.BLUE);contentFormat.setBorder(Border.BOTTOM,BorderLineStyle.THIN,Colour.BLUE);//contentFormat.setBorder(Border.LEFT,BorderLineStyle.THIN,Colour.BLUE);contentFormat.setBackground(Colour.BLUE2);Label label;for (int i = 0; i < title.length; i++) {// Label(列号,行号 ,内容 )label = new jxl.write.Label(i, 4, title[i]); // put the title in// row1label.setCellFormat(contentFormat);sheet.addCell(label);}workbook.write();workbook.close();}catch (Exception e) {e.printStackTrace();}}}

热点排行