import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.PageOrientation;
import jxl.format.PaperSize;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.NumberFormat;
import jxl.write.WritableCellFeatures;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class JXLClass {
public static String savePath = "D:"+java.io.File.separator+"saveDir";
public static void main(String[] args) {
String fileName = System.currentTimeMillis()+".xls";
String filePath = savePath+ java.io.File.separator + fileName;
System.out.println("文件路徑:"+filePath);
File excelFile = new File(filePath);
createExcel(excelFile);
}
public static void createExcel(File file){
System.out.println("生成Excel文件開始了!");
try {
String[] title={"序號","姓名","年齡","工資","出生日期","婚否","頭像"};
WritableWorkbook workBook = Workbook.createWorkbook(file);
WritableSheet sheet = workBook.createSheet("工作表1", 0);
WritableSheet sheet2 = workBook.createSheet("工作表2", 1);
WritableSheet sheet3 = workBook.createSheet("工作表3", 2);
sheet.getSettings().setOrientation(PageOrientation.LANDSCAPE) ;
sheet.getSettings().setPaperSize(PaperSize.A4);
sheet.getSettings().setFitHeight(297);
sheet.getSettings().setFitWidth(210) ;
sheet.getSettings().setTopMargin(0.5) ;
sheet.getSettings().setBottomMargin(0.3) ;
sheet.getSettings().setLeftMargin(0.1) ;
sheet.getSettings().setRightMargin(0.1) ;
sheet.getSettings().getFooter().getCentre().appendPageNumber() ;
sheet.getSettings().setFooterMargin(0.07) ;
sheet.getSettings().setProtected(true) ;
sheet.getSettings().setPassword("123456") ;
sheet.getSettings().setPrintHeaders(true) ;
sheet.mergeCells(0,0,title.length-1,0);
sheet.setRowView(0,500,false);
WritableFont titleFont = new WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,
true,UnderlineStyle.DOUBLE,Colour.RED);
WritableCellFormat cellFormatForTitle = new WritableCellFormat(titleFont);
cellFormatForTitle.setAlignment(jxl.format.Alignment.CENTRE);
cellFormatForTitle.setVerticalAlignment(VerticalAlignment.CENTRE);
cellFormatForTitle.setWrap(true);
cellFormatForTitle.setBorder(Border.ALL, BorderLineStyle.THIN);
cellFormatForTitle.setBackground(Colour.LIGHT_GREEN);
Label lab = new Label(0,0,"JXL練習",cellFormatForTitle);
sheet.addCell(lab);
for(int i=0;i<title.length;i++){
sheet.setColumnView(i, 20);
Label label = new Label(i,1,title[i],cellFormatForTitle);
sheet.addCell(label);
}
sheet.setRowView(1,500,false);
WritableFont bodyFont = new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD,
false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
WritableCellFormat cellFormatForBody = new WritableCellFormat(bodyFont);
cellFormatForBody.setAlignment(jxl.format.Alignment.CENTRE);
cellFormatForBody.setVerticalAlignment(VerticalAlignment.CENTRE);
cellFormatForBody.setWrap(true);
cellFormatForBody.setBorder(Border.ALL, BorderLineStyle.THIN);
cellFormatForBody.setBackground(Colour.LIGHT_GREEN);
Number number = new Number(0,2,1,cellFormatForBody);
sheet.addCell(number);
Label name = new Label(1,2,"張三",cellFormatForBody);
sheet.addCell(name);
Number age = new Number(2,2,10,cellFormatForBody);
sheet.addCell(age);
NumberFormat nf = new NumberFormat("#.##");
WritableCellFormat wcfN = new WritableCellFormat(nf);
wcfN.setAlignment(Alignment.CENTRE);
wcfN.setVerticalAlignment(VerticalAlignment.CENTRE);
wcfN.setWrap(true);
wcfN.setBorder(Border.ALL, BorderLineStyle.THIN);
wcfN.setBackground(Colour.LIGHT_GREEN);
Number salary = new Number(3,2,3000.1415926,wcfN);
sheet.addCell(salary);
DateFormat df = new DateFormat("yyyy-MM-dd hh:mm:ss");
WritableCellFormat wcfDF = new WritableCellFormat(df);
wcfDF.setAlignment(Alignment.CENTRE);
wcfDF.setVerticalAlignment(VerticalAlignment.CENTRE);
wcfDF.setWrap(true);
wcfDF.setBorder(Border.ALL, BorderLineStyle.THIN);
wcfDF.setBackground(Colour.LIGHT_GREEN);
DateTime birthday = new DateTime(4,2,new Date(),wcfDF);
sheet.addCell(birthday);
Label lblColumn = new Label(5, 2, "請選擇",cellFormatForBody);
WritableCellFeatures wcf2 = new WritableCellFeatures();
List angerlist = new ArrayList();
angerlist.add("已婚");
angerlist.add("未婚");
wcf2.setDataValidationList(angerlist);
lblColumn.setCellFeatures(wcf2);
sheet.addCell(lblColumn);
System.out.print("圖片路徑:"+(savePath+ java.io.File.separator+"1.png"));
WritableImage photo=new WritableImage(6,2,1,1,new File(savePath+ java.io.File.separator+"1.png"));
sheet.addImage(photo);
sheet.setRowView(2,400,false);
workBook.write();
workBook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
說明:
1、啟用了密碼保護功能,所以生成的Excel不能編輯,Excel2007可以在”審閱-更改-撤銷工作表保護“模塊取消保護,取消時要求輸入下面設置的密碼123456
- sheet.getSettings().setProtected(true) ;
- sheet.getSettings().setPassword("123456") ;
2、設置行高的問題:
可以使用setRowView(int i,int height)方法來設置行高
- sheet.setRowView(0,500,false);
- 這個方法可以有兩個參數,也可以有三個。
第一個參數是要設置行高的行,
第二個參數是設置行高的高度。這個高度大概得設置成Excel里面行高的20倍才能達到相應的效果。
第三個參數是boolean類型,是否隱藏,默認為隱藏。要是不調用setRowView方法那么當前行不會隱藏,要是調用了且只寫了兩個參數,那么會被隱藏。寫了三個參數時,true為隱藏,false為不隱藏。設置列寬的setColumnView方法跟這個相同。
- 3、圖像類型的數據:
- WritableImage photo=new WritableImage(6,2,1,1,new File(savePath+ java.io.File.separator+"1.png"));
- 這四個參數分別表示:第幾列,第幾行,圖片需要占據幾列,圖片需要占據幾行,圖形文件。需要注意的是第三和第四兩個參數,它們並不是圖片的像數數值,而是占據幾個單元格的意思。
- 標題帶有下拉框的形式:
-
- Label lblColumn = new Label(5, 2, "請選擇",cellFormatForBody);
- WritableCellFeatures wcf2 = new WritableCellFeatures();
- List angerlist = new ArrayList();
- angerlist.add("已婚");
- angerlist.add("未婚");
- wcf2.setDataValidationList(angerlist);
- lblColumn.setCellFeatures(wcf2);
- sheet.addCell(lblColumn);