Java导出Excel
我这里就是举例
还是需要根据实际情况去put值
public static void writeExcelExportData(final PageParameter pp,
IDbDataQuery<PeixunBean> pxsq, InputStream ins, OutputStream out)
throws IOException {
POIFSFileSystem fs = new POIFSFileSystem(ins);
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook(fs);
// 生成一个表格
HSSFSheet sheet = workbook.getSheetAt(0);
// 第三步,在sheet中添加表头第0行
HSSFRow row = sheet.createRow((int) 2);
row.setHeight((short) (20 * 20));
final ArrayList<Map<String, String>> al = new ArrayList<Map<String, String>>();
PeixunBean p = null;
// 遍历行数据
int r = 1;
while ((p = pxsq.next()) != null) {
// 装载导出数据
@SuppressWarnings("unchecked")
Map<String, String> map = new HashedMap();
map.put("xuhao", r + "");
map.put("name", p.getName());
map.put("startDate",
null != p ? Convert.toDateString(p.getStartDate(),
"yyyy-MM-dd") : "");
map.put("endDate",
null != p ? Convert.toDateString(p.getEndDate(),
"yyyy-MM-dd") : "");
map.put("isqdht", p.getIsqdht() == 0 ? "是" : "否");
map.put("phone", p.getPhone());
r++;
al.add(map);
}
int rowlen = 2;
// 遍历导出数据到单元格
for (int i = 0; i < al.size(); i++) {
// 获取每一行的数据
Map<String, String> map = al.get(i);
// 创建行数据 第一行/第二行/...
row = sheet.createRow((int) (rowlen++));
row.setHeight((short) (15 * 20));
// 每一行各个单元格的数据
String[] columnName = {
map.get("xuhao"),
map.get("phone"),
map.get("name"),
map.get("startDate"),
map.get("endDate"),
map.get("isqdht"),
};
// 将数据装载到各个单元格
for (int j = 0; j < 20; j++) {
HSSFCell cell = row.createCell((short) j);
cell.setCellValue(columnName[j]);
}
}
workbook.write(out);
}
}