一个用Java jexcelapi包动态生成excel文件的代码例子
下载最新的JExcelApi ,必须确认是新版本,老版本插入图片似乎有问题
package com.sunrise.psmis.util;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import jxl.CellType;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableFont;
import jxl.write.WritableImage;
import org.apache.commons.lang.StringUtils;
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO
TestMain test=new TestMain();
test.crExcel();
test.genExcel();
test.createExcel();
//test.genPDF();
public void crExcel(){ jxl.write.WritableFont wfBold = new jxl.write.WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false); ws.addCell(new Label(0, 2, "传票编号传票编号传票编号:",wcfFBold)); //可合并分割单元格 // 创建可写入的Excel工作薄对象 // 读取第一张工作表 // 1.添加Label对象 // 写入Excel对象 // 关闭可写入的Excel对象 // 关闭只读的Excel对象 // 创建可写入的Excel工作薄对象 // 读取第一张工作表 // 判断单元格的类型, 做出相应的转化 // 写入Excel对象 // 关闭可写入的Excel对象 // 关闭只读的Excel对象 } Java Excel API is a mature, open source java API enabling developers to read, write, and modifiy Excel spreadsheets dynamically. Now java developers can read Excel spreadsheets, modify them with a convenient and simple API, and write the changes to any output stream (e.g. disk, HTTP, database, or any socket). Any operating system which can run a Java virtual machine (i.e., not just Windows) can both process and deliver Excel spreadsheets. Because it is Java, the API can be invoked from within a servlet, thus giving access to Excel spreadsheets over internet and intranet web applications. The following resources are available:
}
try
{
// 构建Workbook对象, 只读Workbook对象
// Method 1:创建可写入的Excel工作薄
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File("E:/test.xls"));
wwb.setProtected(true);
// 创建Excel工作表
jxl.write.WritableSheet ws = wwb.createSheet("退补电费传票单", 0);
//设置保护单元格,只读不可写
ws.setProtected(true);
//标题
ws.mergeCells(0, 0, 5, 0);//合并单元格
//设置字体,大小,颜色,斜体,黑体
jxl.write.WritableFont wf = new jxl.write.WritableFont(WritableFont.ARIAL, 15, WritableFont.BOLD, false);
jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf);
//设置水平居中
wcfF.setAlignment(Alignment.CENTRE);
jxl.write.Label labelC = new jxl.write.Label(0, 0, "退补电费传票单",wcfF);
ws.addCell(labelC);
//小标题
ws.mergeCells(0, 1, 5, 1);
jxl.write.WritableFont wf2 = new jxl.write.WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false);
jxl.write.WritableCellFormat wcfF2 = new jxl.write.WritableCellFormat(wf2);
wcfF2.setAlignment(Alignment.CENTRE);
jxl.write.Label labelC2 = new jxl.write.Label(0, 1, "广州越秀供电局",wcfF2);
ws.addCell(labelC2);
jxl.write.WritableCellFormat wcfFBold = new jxl.write.WritableCellFormat(wfBold);
//设置垂直居上
wcfFBold.setVerticalAlignment(VerticalAlignment.TOP);
wcfFBold.setShrinkToFit(false);
ws.addCell(new Label(1,2,"023156456"));
ws.addCell(new Label(2, 2, "月份:",wcfFBold));
ws.addCell(new Label(3,2,"200612"));
ws.addCell(new Label(4, 2, "日期:",wcfFBold));
ws.addCell(new Label(5,2,"2006-12-12"));
ws.mergeCells(0, 7, 5, 7);
ws.addCell(new Label(0,7,"非普工业\n非普工业\n非普工业\n非普工业\n非普工业非普工业"));
//添加图片对象,jxl只支持png格式图片
File image = new File("E:/1-我的列表.png");
WritableImage wimage = new WritableImage(10,7,20,20,image);
ws.addImage(wimage);
// 写入Exel工作表
wwb.write();
// 关闭Excel工作薄对象
wwb.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 创建excel工作薄
*
*/
public void createExcel() {
try {
// 创建只读的Excel工作薄的对象
jxl.Workbook rw = jxl.Workbook
.getWorkbook(new File("E:/source.xls"));
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(
"E:/desc.xls"), rw);
jxl.write.WritableSheet ws = wwb.getSheet(0);
// ws.mergeCells(0, 0, 5, 0);
// 获得第一个单元格对象
// jxl.write.WritableCell wc = ws.getWritableCell(0, 0);
jxl.write.Label labelC = new jxl.write.Label(0, 0, "This is a Label cell");
ws.addCell(labelC);
// 添加带有字型Formatting的对象
jxl.write.WritableFont wf = new jxl.write.WritableFont(WritableFont.TIMES, 18, WritableFont.BOLD, true);
jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf);
jxl.write.Label labelCF = new jxl.write.Label(1, 0, "This is a Label Cell", wcfF);
ws.addCell(labelCF);
// 添加带有字体颜色Formatting的对象
jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
jxl.write.Label labelCFC = new jxl.write.Label(1, 0, "This is a Label Cell", wcfFC);
ws.addCell(labelCFC);
// 2.添加Number对象
jxl.write.Number labelN = new jxl.write.Number(0, 1, 3.1415926);
ws.addCell(labelN);
// 添加带有formatting的Number对象
jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf);
jxl.write.Number labelNF = new jxl.write.Number(1, 1, 3.1415926, wcfN);
ws.addCell(labelNF);
// 3.添加Boolean对象
jxl.write.Boolean labelB = new jxl.write.Boolean(0, 2, false);
ws.addCell(labelB);
// 4.添加DateTime对象
jxl.write.DateTime labelDT = new jxl.write.DateTime(0, 3, new java.util.Date());
ws.addCell(labelDT);
// 添加带有formatting的DateFormat对象
jxl.write.DateFormat df = new jxl.write.DateFormat("dd MM yyyy hh:mm:ss");
jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);
jxl.write.DateTime labelDTF = new jxl.write.DateTime(1, 3, new java.util.Date(), wcfDF);
ws.addCell(labelDTF);
wwb.write();
wwb.close();
rw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 拷贝、更新工作薄
*
*/
public void genExcel() {
try {
// 创建只读的Excel工作薄的对象
jxl.Workbook rw = jxl.Workbook
.getWorkbook(new File("E:/test.xls"));
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(
"E:/desc.xls"), rw);
jxl.write.WritableSheet ws = wwb.getSheet(0);
// 获得第一个单元格对象
jxl.write.WritableCell wc = ws.getWritableCell(0, 0);
if (wc.getType() == CellType.LABEL) {
Label l = (Label) wc;
l.setString("The value has been modified." + wc.getContents());
}
wwb.write();
wwb.close();
rw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Some Features
Resources
tutorial
tech notes