package cn.com.burgeon.excel;
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException; java
public class Jxltest { 字體
public void crExcel() {
try {
WritableWorkbook wwb = Workbook.createWorkbook(new File(
"d:/test.xls"));
// 定義只讀
// wwb.setProtected(true);
WritableSheet wa=null;
int sheetcount=200;
int allsheetcount=1001;
int sheetindex=0;
if(allsheetcount%sheetcount!=0){
sheetindex=allsheetcount/sheetcount+1;
}else {
sheetindex=allsheetcount/sheetcount;
}
for (int j = 0; j < sheetindex; j++) {
wa = wwb.createSheet("定貨會款"+j, j);
// 設置寬和高
// wa.setColumnView(0, 18);
// wa.setRowView(0, 280);
// 合併單元格注意mergeCells(col0,row0,col1,row1) --列從0開始,col1爲你要合併到第幾列,行也同樣 excel
// wa.setProtected(false);
// 設置 字體,大小,顏色,斜體,黑體
WritableFont wf = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.BOLD, false);
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setAlignment(Alignment.CENTRE);
wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
// 設置垂直居上
// wa.setColumnView(1,19);
/*
* Label label4=new Label(3,1,"39",wcf); Label label5=new
* Label(4,1,"40",wcf); Label label6=new Label(5,1,"41",wcf); Label
* label7=new Label(6,1,"42",wcf); Label label8=new
* Label(7,1,"43",wcf); wa.addCell(label4); wa.addCell(label5);
* wa.addCell(label6); wa.addCell(label7); wa.addCell(label8);
*/
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
//先循環上面四列,如果4的倍數,換行
int rowscount=0;//若爲4的倍數,換行,rowcount+1
int sheetSize=allsheetcount-j*200;
if(sheetSize>sheetcount){
sheetSize=sheetcount;
}
for(int i=0;i<sheetSize;i++){
if(i!=0&&i%4==0){
rowscount+=1;
}
int colsindex=i%4;//獲得列索引 cols縱 rows橫 cols終止位置 rows 終止位置
int rows=(rowscount*8)>0?(rowscount*8):0; //行座標
int cols=(colsindex*5)>0?(colsindex*5):0; //列座標
wa.mergeCells(cols+0, rows+0, cols+0, rows+0);
wa.mergeCells(cols+1, rows+0, cols+2, rows+0);
wa.mergeCells(cols+3, rows+0, cols+3, rows+0);
wa.mergeCells(cols+0, rows+1, cols+0, rows+1);
wa.mergeCells(cols+1, rows+1, cols+2, rows+1);
wa.mergeCells(cols+3, rows+1, cols+3, rows+1);
wa.mergeCells(cols+4, rows+1, cols+4, rows+1);
wa.mergeCells(cols+0, rows+2, cols+4, rows+7);
Label label = new Label(cols+0, rows+0, "款號", wcf);
wa.addCell(label);
Label label1 = new Label(cols+1, rows+0, "品名", wcf);
wa.addCell(label1);
Label label2 = new Label(cols+3, rows+0, "色號", wcf);
wa.addCell(label2);
Label label3 = new Label(cols+4, rows+0, "零售價", wcf);
wa.addCell(label3);
Label label4 = new Label(cols+0, rows+1, "DC3-1", wcf);
wa.addCell(label4);
Label label5 = new Label(cols+1, rows+1, "女款", wcf);
wa.addCell(label5);
Label label6 = new Label(cols+3, rows+1, "-", wcf);
wa.addCell(label6);
Label label7 = new Label(cols+4, rows+1, "880.00", wcf);
wa.addCell(label7);
File image = new File("d:/as0011.png");
// 只支持png格式的 圖形 參數 分別表示 開始列 行 寬 高
WritableImage wimage = new WritableImage(cols+1, rows+2, 2, 6, image);
WritableCellFormat wcf1 = new WritableCellFormat();
wcf1.setBorder(Border.ALL, BorderLineStyle.THIN);
wa.getWritableCell(0, 3).setCellFormat(wcf1);
wa.addImage(wimage);
}
}
/* wa.mergeCells(0, 0, 0, 0);
wa.mergeCells(1, 0, 2, 0);
wa.mergeCells(3, 0, 3, 0);
wa.mergeCells(0, 1, 0, 1);
wa.mergeCells(1, 1, 2, 1);
wa.mergeCells(3, 1, 3, 1);
wa.mergeCells(4, 1, 4, 1);
wa.mergeCells(0, 2, 4, 7);
Label label = new Label(0, 0, "款號", wcf);
wa.addCell(label);
Label label1 = new Label(1, 0, "品名", wcf);
wa.addCell(label1);
Label label2 = new Label(3, 0, "色號", wcf);
wa.addCell(label2);
Label label3 = new Label(4, 0, "零售價", wcf);
wa.addCell(label3);
Label label4 = new Label(0, 1, "DC3-1", wcf);
wa.addCell(label4);
Label label5 = new Label(1, 1, "女款", wcf);
wa.addCell(label5);
Label label6 = new Label(3, 1, "-", wcf);
wa.addCell(label6);
Label label7 = new Label(4, 1, "880.00", wcf);
wa.addCell(label7);
File image = new File("d:/as0011.png");
// 只支持png格式的 圖形 參數 分別表示 開始列 行 寬 高
WritableImage wimage = new WritableImage(1, 2, 2, 6, image);
WritableCellFormat wcf1 = new WritableCellFormat();
wcf1.setBorder(Border.ALL, BorderLineStyle.THIN);
wa.getWritableCell(0, 3).setCellFormat(wcf1);
wa.addImage(wimage);*/ orm
wwb.write();
wwb.close();
} catch (IOException ie) {
ie.printStackTrace();
} catch (WriteException we) {
we.printStackTrace();
}
} 索引
public static void main(String[] args) {
new Jxltest().crExcel();
} get
} it