package util;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class XlsProc {
private String sheetName = null;
private WritableCellFormat wcfTitle = null;
private WritableCellFormat wcfMainSingle = null;
private WritableCellFormat wcfMainDouble = null;
public XlsProc(){
this.sheetName = "信息导出_FORM_A";
}
private void init(){
WritableFont wfTemp = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.YELLOW);
try {
wcfTitle = new WritableCellFormat(wfTemp);
wcfTitle.setAlignment(jxl.format.Alignment.CENTRE);
wcfTitle.setBackground(jxl.format.Colour.BLUE);
wcfTitle.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.DOTTED, jxl.format.Colour.BLUE);
wfTemp = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
wcfMainSingle = new WritableCellFormat(wfTemp);
wcfMainSingle.setAlignment(jxl.format.Alignment.CENTRE);
//wcfMainSingle.setBackground(jxl.format.Colour.GREY_25_PERCENT);
wcfMainSingle.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.DOTTED, jxl.format.Colour.BLUE);
wfTemp = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
wcfMainDouble = new WritableCellFormat(wfTemp);
wcfMainDouble.setAlignment(jxl.format.Alignment.CENTRE);
wcfMainDouble.setBackground(jxl.format.Colour.GREY_25_PERCENT);
wcfMainDouble.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.DOTTED, jxl.format.Colour.BLUE);
} catch (WriteException e) {
System.out.println("WriteException in init WritableFont");
e.printStackTrace();
}
}
public void expXlsForAry(OutputStream os, String[] titleAry, String[][] dataAry) throws IOException{
this.init();
WritableWorkbook wwb = Workbook.createWorkbook(os);
WritableSheet ws = wwb.createSheet(this.sheetName, 0);
this.expXls(ws,titleAry,dataAry);
wwb.write();
try {
wwb.close();
} catch (WriteException e) {
System.out.println("jxl异常:jxl关闭时出现意外!" + e.getMessage());
}
}
private void expXls(WritableSheet ws, String[] titleAry, String[][] dataAry){
Label labelCF = null;
WritableCellFormat curFormat = null;
labelCF = new Label(0, 0, "序号" ,this.wcfTitle);
try {
ws.addCell(labelCF);
for(int i=1; i<=titleAry.length; i++){
labelCF = new Label(i, 0, titleAry[i-1],this.wcfTitle);
ws.addCell(labelCF);
}
for(int i=1; i<=dataAry.length; i++){
for(int j=0; j<=dataAry[i-1].length; j++){
if (i%2!=0) curFormat = this.wcfMainSingle;
else curFormat = this.wcfMainDouble;;
if (j==0) labelCF = new Label(j, i, i+"", curFormat);
else labelCF = new Label(j, i, dataAry[i-1][j-1], curFormat);
ws.addCell(labelCF);
}
}
} catch (RowsExceededException e) {
System.out.println("jxl异常:行数超出应有范围!" + e.getMessage());
} catch (WriteException e) {
System.out.println("jxl异常:写excel文件时发生意外!" + e.getMessage());
}
}
}
posted on 2009-03-29 19:54
雨飞 阅读(212)
评论(0) 编辑 收藏