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);