//调用方法是代码
List<String> infodList = getInfoFieldList(userid, propertyId, 3);
li.add(infodList);
String fileName = "查询统计_" + System.currentTimeMillis() + ".xls";
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader(
"Content-Disposition",
"attachment;filename="
+ URLEncoder.encode(fileName, "UTF-8"));
InspectUtil.toExcelExport(response.getOutputStream(), li);
//
正式代码
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public static void toExcelExport(OutputStream os, List<List<String>> li) {
// 创建工作薄
WritableWorkbook workbook = null;
try {
workbook = Workbook.createWorkbook(os);
} catch (IOException e) {
e.printStackTrace();
}
// 创建新的一页
WritableSheet sheet = workbook.createSheet("查询统计", 0);
WritableCellFormat titleFormat1 = new WritableCellFormat();
try {
titleFormat1.setVerticalAlignment(VerticalAlignment.CENTRE); // 设置居中对齐
titleFormat1.setAlignment(Alignment.CENTRE);// 设置居中对齐(这俩哪个是上下/左右对齐也没验证过)
titleFormat1.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.THIN);// 给单元格加边
sheet.mergeCells(0, 0, 0, 1);
sheet.mergeCells(1, 0, 1, 1);
Label rown = new Label(0, 0, "序号", titleFormat1);
sheet.addCell(rown);
rown = new Label(1, 0, "单位", titleFormat1);
sheet.addCell(rown);
List<String> theads = li.get(li.size() - 1);
int j = 2;
for (int i = 0; i < theads.size(); i++) {
sheet.mergeCells(j, 0, j + 3, 0);
rown = new Label(j, 0, theads.get(i), titleFormat1);
sheet.addCell(rown);
// 写入/不完整数/完整率(%)/不正确数/正确率(%)--标题
rown = new Label(j, 1, "不完整数", titleFormat1);
sheet.addCell(rown);
rown = new Label(j + 1, 1, "完整率(%)", titleFormat1);
sheet.addCell(rown);
rown = new Label(j + 2, 1, "不正确数", titleFormat1);
sheet.addCell(rown);
rown = new Label(j + 3, 1, "正确率(%)", titleFormat1);
sheet.addCell(rown);
j += 4;
}
// 写入数据
for (int i = 0; i < li.size() - 1; i++) {
for (int t = 0; t < li.get(i).size(); t++) {
rown = new Label(t, i + 2, li.get(i).get(t), titleFormat1);
sheet.addCell(rown);
}
}
workbook.write();
workbook.close();
os.close();
} catch (WriteException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
java response to excel (create excel)
最新推荐文章于 2024-01-04 11:18:36 发布