package com.sinosoft.utils;
import java.io.File;
import java.util.List;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExportFileUtil {
public static void exportExcelBaseInfo(List<Object[]> list,String filePath,String fileName,
String widths,String headers) throws Exception {
try {
String[] width = widths.split(";");
String[] header = headers.split(";");
/**
* 设置标题字体样式
*/
WritableFont wfTitle = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.BOLD, false,
UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
/**
* 设置列表字体样式
*/
WritableFont wfBody = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD,
false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat wcfTitle = new WritableCellFormat(wfTitle);
WritableCellFormat wcfBody = new WritableCellFormat(wfBody);
wcfTitle.setAlignment(jxl.format.Alignment.CENTRE);
wcfBody.setAlignment(jxl.format.Alignment.CENTRE);
File file = new File(filePath+fileName);
WritableWorkbook workbook = Workbook.createWorkbook(file);
workbook.createSheet("book1", 0);
WritableSheet sheet = workbook.getSheet(0);
Label label = new Label(0, 0, "序号", wcfTitle);
sheet.addCell(label);
/**设置标题列**/
for (int i = 0; i < header.length; i++) {
label = new Label((i + 1), 0, header[i], wcfTitle);
sheet.addCell(label);
/**设置列宽**/
sheet.setColumnView((i + 1), Integer.parseInt(width[i]) / 3);
}
/** 输入列值 **/
for (int j = 0; j < list.size(); j++) {
Object[] result = list.get(j);
label = new Label(0, (j + 1), (j + 1) + "", wcfBody);
sheet.addCell(label);
/**输入行值**/
for (int i = 0; i < result.length ; i++) {
Object temp = result[i];
if (temp==null) {
temp ="";
}
label = new Label((i + 1), (j + 1), temp.toString(), wcfBody);
sheet.addCell(label);
}
}
workbook.write();
workbook.close();
} catch (RuntimeException e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.util.List;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExportFileUtil {
public static void exportExcelBaseInfo(List<Object[]> list,String filePath,String fileName,
String widths,String headers) throws Exception {
try {
String[] width = widths.split(";");
String[] header = headers.split(";");
/**
* 设置标题字体样式
*/
WritableFont wfTitle = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.BOLD, false,
UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
/**
* 设置列表字体样式
*/
WritableFont wfBody = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD,
false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat wcfTitle = new WritableCellFormat(wfTitle);
WritableCellFormat wcfBody = new WritableCellFormat(wfBody);
wcfTitle.setAlignment(jxl.format.Alignment.CENTRE);
wcfBody.setAlignment(jxl.format.Alignment.CENTRE);
File file = new File(filePath+fileName);
WritableWorkbook workbook = Workbook.createWorkbook(file);
workbook.createSheet("book1", 0);
WritableSheet sheet = workbook.getSheet(0);
Label label = new Label(0, 0, "序号", wcfTitle);
sheet.addCell(label);
/**设置标题列**/
for (int i = 0; i < header.length; i++) {
label = new Label((i + 1), 0, header[i], wcfTitle);
sheet.addCell(label);
/**设置列宽**/
sheet.setColumnView((i + 1), Integer.parseInt(width[i]) / 3);
}
/** 输入列值 **/
for (int j = 0; j < list.size(); j++) {
Object[] result = list.get(j);
label = new Label(0, (j + 1), (j + 1) + "", wcfBody);
sheet.addCell(label);
/**输入行值**/
for (int i = 0; i < result.length ; i++) {
Object temp = result[i];
if (temp==null) {
temp ="";
}
label = new Label((i + 1), (j + 1), temp.toString(), wcfBody);
sheet.addCell(label);
}
}
workbook.write();
workbook.close();
} catch (RuntimeException e) {
e.printStackTrace();
}
}
}