@RequestInfo(comments = "导入Excel ", date = "2019-04-17 15:52:22", author = "wangs", remark = "additional description")
public void exportExcel(){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String title = df.format(new Date());
File file = new File(title + ".xls");
// 创建工作薄
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
// 创建工作薄中的工作表 sheet:一张表的简称
HSSFSheet hssfSheet = hssfWorkbook.createSheet();
// 创建行
HSSFRow row = hssfSheet.createRow(0);
// 创建单元格
HSSFCell cell = null;
// 初始化索引
int rowIndex = 0;
int cellIndex = 0;
String[] headers = {"源表表名","数据仓英文表名","描述", "源库数据量","源库待同步数据量","统计时间"};
Zl53 zl53 = getModel(Zl53.class);
List<Zl53> list = zl53.find("select * from zl53");
// 创建标题行
row = hssfSheet.createRow(rowIndex);
rowIndex++;
// 遍历标题
for (String h : headers) {
//创建列
cell = row.createCell(cellIndex);
//索引递增
cellIndex++;
//逐列插入标题
cell.setCellValue(h);
}
// 得到所有记录 行:列
Zl53 record = null;
if (list != null) {
// 获取所有的记录 有多少条记录就创建多少行
for (int i = 0; i < list.size(); i++) {
row = hssfSheet.createRow(rowIndex);
// 得到所有的行 一个record就代表 一行
record = list.get(i);
//下一行索引
rowIndex++;
//刷新新行索引
cellIndex = 0;
// 在有所有的记录基础之上,便利传入进来的表头,再创建N行
for (String h : headers) {
cell = row.createCell(cellIndex);
cellIndex++;
//按照每条记录匹配数据
cell.setCellValue(record.get(h) == null ? "" : record.get(h).toString());
}
}
}
try {
FileOutputStream fileOutputStreane = new FileOutputStream(file);
hssfWorkbook.write(fileOutputStreane);
fileOutputStreane.flush();
fileOutputStreane.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
renderFile(file);
}
沐白白 2019/4/17 22:20:48
@RequestInfo(comments = "导入Excel ", date = "2019-04-17 15:52:22", author = "wangs", remark = "additional description")
public void exportExcel(){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String title = df.format(new Date());
File file = new File(title + ".xls");
// 创建工作薄
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
// 创建工作薄中的工作表 sheet:一张表的简称
HSSFSheet hssfSheet = hssfWorkbook.createSheet();
// 创建行
HSSFRow row = hssfSheet.createRow(0);
// 创建单元格
HSSFCell cell = null;
// 初始化索引
int rowIndex = 0;
int cellIndex = 0;
String[] headers = {"源表表名","数据仓英文表名","描述", "源库数据量","源库待同步数据量","统计时间"};
Zl53 zl53 = getModel(Zl53.class);
List<Zl53> list = zl53.find("select * from zl53");
// 创建标题行
row = hssfSheet.createRow(rowIndex);
rowIndex++;
// 遍历标题
for (String h : headers) {
//创建列
cell = row.createCell(cellIndex);
//索引递增
cellIndex++;
//逐列插入标题
cell.setCellValue(h);
}
// 得到所有记录 行:列
Zl53 record = null;
if (list != null) {
// 获取所有的记录 有多少条记录就创建多少行
for (int i = 0; i < list.size(); i++) {
row = hssfSheet.createRow(rowIndex);
// 得到所有的行 一个record就代表 一行
record = list.get(i);
//下一行索引
rowIndex++;
//刷新新行索引
cellIndex = 0;
// 在有所有的记录基础之上,便利传入进来的表头,再创建N行
for (String h : headers) {
cell = row.createCell(cellIndex);
cellIndex++;
//按照每条记录匹配数据
cell.setCellValue(record.get(h) == null ? "" : record.get(h).toString());
}
}
}
try {
FileOutputStream fileOutputStreane = new FileOutputStream(file);
hssfWorkbook.write(fileOutputStreane);
fileOutputStreane.flush();
fileOutputStreane.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
renderFile(file);
}