- 论坛徽章:
- 0
|
作者:dxadnwfn(gong c w)
版权:"厕所里冥想"
上传/更新时间:2007-08-02
功能:excel 模板化的写入
package com.ivsoftware.common.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
/**
*
* @author dxadnwfn
*
* 有模板的形式写excel.
*
* 作用:
*
* 1.用助于复杂的excel的操作.如果字体大小,格式等.
*
* 2.简化操作
*
* 3.所见即所得.模板定的样式就是最后输出样式.
*
* 缺点:
*
* 1.表格的定位还是要自己去写,这里较烦琐
*
* 2.正因为定位要自己写,造成程序无法成为真正的公共类.
*/
public class WriteExcel {
/**
*
* @param modlePath
* 需要读取的excel路径[模板]
* @param outName
* 写出的完整路径
* @param sheetName
* sheet名字
* @param contractList
* 数据列表
*/
public static void writeExcel(String modlePath, String outName,
String sheetName, List dateList) {
WritableWorkbook wwb = null;
Workbook workbook = null;
try {
try {
// 根据模板去创建新的工作簿
InputStream ins = new FileInputStream(modlePath);
workbook = Workbook.getWorkbook(ins);
File outFile = new File(outName);
wwb = Workbook.createWorkbook(outFile, workbook);
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (wwb != null) {
try {
// 添加带有formatting的Number对象
jxl.write.NumberFormat nf = new jxl.write.NumberFormat(
"#.##");
jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat();
wcfN.setWrap(true);
wcfN.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.THIN);
wcfN.setAlignment(jxl.format.Alignment.CENTRE);
wcfN
.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
// 添加带有formatting的DateFormat对象
jxl.write.DateFormat df = new jxl.write.DateFormat(
"yyyy-mm-dd");
jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(
df);
wcfDF.setWrap(true);
wcfDF.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.THIN);
wcfDF.setAlignment(jxl.format.Alignment.CENTRE);
wcfDF
.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
// 字段字体.
jxl.write.WritableFont wfc1 = new jxl.write.WritableFont(
WritableFont.COURIER, 10, WritableFont.BOLD, true);
jxl.write.WritableCellFormat wcfFC1 = new jxl.write.WritableCellFormat();
wcfFC1.setWrap(true);
wcfFC1.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.THIN);
wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);
wcfFC1
.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
// 得到工作表
WritableSheet wsheet = wwb.getSheet(sheetName);
if (dateList != null) {
int i = 2;
int ii = 1;
for (Iterator itr = dateList.iterator(); itr.hasNext();) {
WriteExcelForm contract = (WriteExcelForm) itr
.next();
wsheet
.addCell(new jxl.write.Number(0, i, ii,
wcfN));
wsheet.addCell(new Label(3, i, contract
.getContractNO(), wcfFC1));
wsheet.addCell(new Label(6, i, contract
.getContractName(), wcfFC1));
wsheet.addCell(new Label(11, i, contract
.getProjectName(), wcfFC1));
wsheet.addCell(new Label(17, i, contract
.getContractClassName(), wcfFC1));
wsheet.addCell(new Label(23, i, contract
.getCreateDate(), wcfFC1));
// 设置行高
wsheet.setRowView(i, 800);
i++;
ii++;
}
}
wwb.write();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
System.out.print("写入Excel出错");
e.printStackTrace();
} finally {
try {
workbook.close();
wwb.close();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 测试
*
* @param args
*/
public static void main(String[] args) {
// File.separator 文件分隔符
String localFilePath = "E:" + File.separator + "test" + File.separator;
String localExelPath = "E:" + File.separator + "test" + File.separator
+ "excelmodel" + File.separator;
String folderPath = "";
// excel名字
String excelName = "contractList";
// sheet名字
String sheetName = "合同列表";
// 需要读取的excel路径[模板路径]
String fileName = localExelPath + excelName + ".xls";
// excel保存路径
folderPath = localFilePath + "contractList" + File.separator + "合同列表"
+ ".xls";
// 这里只是为了保证程序可以运行,所以数据为null.真实程序数据放到 封装WriteExcelForm的List里
List contractList = new Vector();
// 写文件到excel文件里
WriteExcel.writeExcel(fileName, folderPath, sheetName, contractList);
}
}
Form:
-----------------------------------
package com.ivsoftware.common.excel;
public class WriteExcelForm {
private String contractID;
private String contractNO;
private String contractName;
private String projectID;
private String projectName;
private String contractClassID;
private String contractClassName;
private String createDate;
private String updateDate;
public String getContractID() {
return contractID;
}
public void setContractID(String contractID) {
this.contractID = contractID;
}
public String getContractNO() {
return contractNO;
}
public void setContractNO(String contractNO) {
this.contractNO = contractNO;
}
public String getContractName() {
return contractName;
}
public void setContractName(String contractName) {
this.contractName = contractName;
}
public String getProjectID() {
return projectID;
}
public void setProjectID(String projectID) {
this.projectID = projectID;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getContractClassID() {
return contractClassID;
}
public void setContractClassID(String contractClassID) {
this.contractClassID = contractClassID;
}
public String getContractClassName() {
return contractClassName;
}
public void setContractClassName(String contractClassName) {
this.contractClassName = contractClassName;
}
public String getCreateDate() {
return createDate;
}
public void setCreateDate(String createDate) {
this.createDate = createDate;
}
public String getUpdateDate() {
return updateDate;
}
public void setUpdateDate(String updateDate) {
this.updateDate = updateDate;
}
}
------------------------------------
excel模板:
![]()
blog日志,注意行版权哦,转载请注明
作者:dxadnwfn(gong c w)
版权:"厕所里冥想"
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/17667/showart_352278.html |
|