package web.code.excel;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.NumberFormat;
import jxl.write.Number;
import jxl.write.Boolean;
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 ExcelWriting {

public WritableWorkbook wwb;
public WritableSheet ws;

public ExcelWriting() {
this.getWriteWorkBook();//得到WorkBook
this.getWritableSheet();//得到Sheet
} public void getWriteWorkBook(){
try{
/*构建Workbook对象, 只读Workbook对象
Method 1:创建可写入的Excel工作薄*/
// File file=new File("C:\\abcde");
// wwb = Workbook.createWorkbook(file); /*Method 2:将WritableWorkbook直接写入到输出流*/
OutputStream os = new FileOutputStream("C:\\aa.xls");
wwb = Workbook.createWorkbook(os); }catch (Exception e){
e.printStackTrace();
}
}

public void getWritableSheet(){//创建Excel工作表
ws = wwb.createSheet("Sheet1", 0);
}

public void closeWorkBook(){//当任务结束后要关闭WorkBook
try {
wwb.close();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void writeDateToExcel() throws RowsExceededException, WriteException{
//1/.添加Label对象
Label labelC = new Label(0, 0, "This is a Label cell");
ws.addCell(labelC);
// 添加带有字型Formatting的对象
WritableFont wf = new WritableFont(WritableFont.TIMES, 18,WritableFont.BOLD, true);
WritableCellFormat wcfF = new WritableCellFormat(wf);
Label labelCF = new Label(1, 0, "This is a Label Cell", wcfF);
ws.addCell(labelCF);
// 添加带有字体颜色Formatting的对象
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 10,
WritableFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
Label labelCFC = new Label(1, 0, "This is a Label Cell", wcfFC);
ws.addCell(labelCFC);
// 2.添加Number对象
Number labelN = new Number(0, 1, 3.1415926);
ws.addCell(labelN);
// 添加带有formatting的Number对象
NumberFormat nf = new NumberFormat("#.##");
WritableCellFormat wcfN = new WritableCellFormat(nf);
Number labelNF = new Number(1, 1, 3.1415926, wcfN);
ws.addCell(labelNF);
// 3.添加Boolean对象
Boolean labelB = new Boolean(0, 2, false);
ws.addCell(labelB);
// 4.添加DateTime对象
DateTime labelDT = new DateTime(0, 3, new java.util.Date());
ws.addCell(labelDT);
// 添加带有formatting的DateFormat对象
DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss");
WritableCellFormat wcfDF = new WritableCellFormat(df);
DateTime labelDTF = new DateTime(1, 3, new java.util.Date(), wcfDF);
ws.addCell(labelDTF);
}

public static void main(String[] args) {
ExcelWriting ew = new ExcelWriting();
// try {
// ew.writeDateToExcel();
// } catch (RowsExceededException e) {
// e.printStackTrace();
// } catch (WriteException e) {
// e.printStackTrace();
// }finally{
ew.closeWorkBook();
// } }}