【文件属性】:
文件名称:创建或读取Excel表
文件大小:6KB
文件格式:JAVA
更新时间:2015-08-28 05:37:56
java,Excel
public static int createdExcel(String PATH, List list, String title, String[] rowsName, String merged) {
try {
File myFile = new File(PATH);
if (!myFile.exists()) {
myFile.createNewFile();
}
WritableWorkbook wbook = Workbook.createWorkbook(myFile); // 创建一个可写返回工作薄同给定文件名
WritableSheet wsheet = wbook.createSheet(title, 0); // sheet名称
// 设置字体
WritableFont wfont = new WritableFont(WritableFont.createFont("宋体"), 10,
WritableFont.BOLD);
WritableCellFormat wcfFC = new WritableCellFormat(wfont);
wcfFC.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); //单元格边缘线格式设置
wcfFC.setAlignment(jxl.format.Alignment.CENTRE); // 居中对齐
wcfFC.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //垂直居中
//wcfFC.setBackground(jxl.format.Colour.BLUE); // 蓝色底
// 设置行高和列宽
//wsheet.setColumnView(列数, 列宽);
//wsheet.setRowView(行数, 行高);
// 开始生成主体内容
for (int i = 0 ; i < rowsName.length; i++) {
wsheet.addCell(new Label(i, 0, rowsName[i], wcfFC));
wsheet.setColumnView(i, 12);
}
wfont = new WritableFont(WritableFont.createFont("宋体"), 10,
WritableFont.NO_BOLD);
wcfFC = new WritableCellFormat(wfont);
wcfFC.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); //单元格边缘线格式设置
//是数字时的格式化
// jxl.write.NumberFormat numberFormat = new jxl.write.NumberFormat(NumberFormat.CURRENCY_DOLLAR);
// jxl.write.WritableCellFormat wcfFCNUMBER = new jxl.write.WritableCellFormat(wfont,numberFormat);
// wcfFCNUMBER.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); //单元格边缘线格式设置
// wcfFCNUMBER.setAlignment(jxl.format.Alignment.CENTRE); // 居中对齐
// wcfFCNUMBER.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //垂直居中
//是数字时的格式化
wcfFC.setWrap(true);
//合并单元格操作
//wsheet.mergeCells(坐标列1, 坐标行1, 坐标列2, 坐标行2)
for (int i = 0; i < list.size(); i++) {
String[] args = (String[]) list.get(i);
for (int j = 0; j < args.length; j ++) {
if (args[j].length()<15 ) {
wsheet.addCell(new jxl.write.Number(j, i+1, Float.parseFloat(args[j]), wcfFC));
} else {
wsheet.addCell(new Label(j, i+1, args[j], wcfFC));
}
}
if (merged.indexOf("," + (i+1) + ",") >=0) {
wsheet.mergeCells(0, i+1, args.length-1, i+1);
wsheet.setRowView(i, 1000);
}
//打印分页符
if (i % 20 == 0) {
//wsheet.addRowPageBreak(i);
}
}
// 主体内容生成结束
wbook.write(); // 写入文件
wbook.close();
return 1;
} catch (Exception ex) {
ex.printStackTrace();
return 0;
}
}