/**
* 创建Excel,并写入数据
*
* @param pname
* @param cityName
* @param adname
* @param list
* @throws FileNotFoundException
*/
public static void createExcel(String pname, String cityName, String adname, List<PageData> list)
throws FileNotFoundException {
XSSFWorkbook xWorkbook = null;
String filePath = "D:\\dataExcel\\" + pname;
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
String filePath3 = filePath + "\\" + cityName;
File file3 = new File(filePath3);
if (!file3.exists()) {
file3.mkdirs();
}
String filePath2 = filePath3 + "\\" + adname + ".xlsx";
File file2 = new File(filePath2);
if (!file2.exists()) {// 不存在,创建
try {
file2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else {
return;
}
FileOutputStream os = new FileOutputStream(filePath2);
try {
// 创建表格工作空间
xWorkbook = new XSSFWorkbook();
// 创建一个新表格
XSSFSheet xSheet = xWorkbook.createSheet("信息表");
// set Sheet页头部
setSheetHeader(xWorkbook, xSheet);
// set Sheet页内容
setSheetContent(xWorkbook, xSheet, list);
xWorkbook.write(os);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != os) {
try {
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (null != xWorkbook) {
try {
xWorkbook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 配置Excel表格的顶部信息,如:学号 姓名 年龄 出生年月
*
* @param xWorkbook
* @param xSheet
*/
private static void setSheetHeader(XSSFWorkbook xWorkbook, XSSFSheet xSheet) {
// 设置表格的宽度 xSheet.setColumnWidth(0, 20 * 256); 中的数字 20 自行设置为自己适用的
xSheet.setColumnWidth(0, 20 * 556);
xSheet.setColumnWidth(1, 20 * 856);
xSheet.setColumnWidth(2, 20 * 556);
xSheet.setColumnWidth(3, 20 * 556);
xSheet.setColumnWidth(4, 20 * 856);
xSheet.setColumnWidth(5, 20 * 856);
xSheet.setColumnWidth(6, 20 * 356);
// 创建表格的样式
CellStyle cs = xWorkbook.createCellStyle();
// 设置水平、垂直居中
cs.setAlignment(CellStyle.ALIGN_CENTER);
cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
// 设置字体
Font headerFont = xWorkbook.createFont();
headerFont.setFontHeightInPoints((short) 12);
/* headerFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); */
headerFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
headerFont.setFontName("宋体");
cs.setFont(headerFont);
cs.setWrapText(true);// 是否自动换行
// 创建一行
XSSFRow xRow0 = xSheet.createRow(0);
// 设置每一列
XSSFCell xCell0 = xRow0.createCell(0);
xCell0.setCellStyle(cs);
xCell0.setCellValue("编号");
XSSFCell xCell1 = xRow0.createCell(1);
xCell1.setCellStyle(cs);
xCell1.setCellValue("名称");
XSSFCell xCell2 = xRow0.createCell(2);
xCell2.setCellStyle(cs);
xCell2.setCellValue("省");
XSSFCell xCell3 = xRow0.createCell(3);
xCell3.setCellStyle(cs);
xCell3.setCellValue("市");
XSSFCell xCell4 = xRow0.createCell(4);
xCell4.setCellStyle(cs);
xCell4.setCellValue("区/县");
XSSFCell xCell5 = xRow0.createCell(5);
xCell5.setCellStyle(cs);
xCell5.setCellValue("地址");
XSSFCell xCell6 = xRow0.createCell(6);
xCell6.setCellStyle(cs);
xCell6.setCellValue("电话");
}
/**
* 配置(赋值)表格内容部分
*
* @param xWorkbook
* @param xSheet
* @throws Exception
*/
private static void setSheetContent(XSSFWorkbook xWorkbook, XSSFSheet xSheet, List<PageData> list)
throws Exception {
// 创建内容样式(头部以下的样式)
CellStyle cs = xWorkbook.createCellStyle();
cs.setWrapText(true);
// 设置水平垂直居中
cs.setAlignment(CellStyle.ALIGN_CENTER);
cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
if (null != list && list.size() > 0) {
System.out.println("总条数:==" + list.size());
for (int i = 0; i < list.size(); i++) {
XSSFRow xRow = xSheet.createRow(i + 1);
// 设置第一列值
XSSFCell xCell0 = xRow.createCell(0);
xCell0.setCellStyle(cs);
xCell0.setCellValue(list.get(i).get("DATA_ID").toString());
// 设置第二列值
XSSFCell xCell1 = xRow.createCell(1);
xCell1.setCellStyle(cs);
xCell1.setCellValue(list.get(i).get("DATA_NAME").toString());
// 设置第三列值
XSSFCell xCell2 = xRow.createCell(2);
xCell2.setCellStyle(cs);
xCell2.setCellValue(list.get(i).get("PNAME").toString());
// 设置第四列值
XSSFCell xCell3 = xRow.createCell(3);
xCell3.setCellStyle(cs);
xCell3.setCellValue(list.get(i).get("CITY_NAME").toString());
// 设置第五列值
XSSFCell xCell4 = xRow.createCell(4);
xCell4.setCellStyle(cs);
xCell4.setCellValue(list.get(i).get("ADNAME").toString());
// 设置第6列值
XSSFCell xCell5 = xRow.createCell(5);
xCell5.setCellStyle(cs);
if (IsNull.paramsIsNull(list.get(i).get("ADDRESS"))) {
xCell5.setCellValue("无");
} else {
xCell5.setCellValue(list.get(i).get("ADDRESS").toString());
}
// 设置第7列值
XSSFCell xCell6 = xRow.createCell(6);
xCell6.setCellStyle(cs);
if (IsNull.paramsIsNull(list.get(i).get("DATA_TEL"))) {
xCell6.setCellValue("无");
} else {
xCell6.setCellValue(list.get(i).get("DATA_TEL").toString());
}
}
}
}