先看一下數據庫和時間戳的轉換
java代碼
@Test
public void test2() throws Exception {
List<Ceshi> ceshiAll = sysCeshiService.getCeshiAll();
ceshiAll.forEach(e->e.setUpdateTime(FormatDateUtil.stampToTime(Long.parseLong(e.getUpdateTime()))));
//標題 表名 導出類型 HSSF xls XSSF xlsx
ExportParams exportParams = new ExportParams("測試表","ceshi", ExcelType.XSSF);
//1.導出參數對象 1.普通Java類的類對象(要導出的實體類的類對象) 3.一個集合 查詢全部的用戶信息
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Ceshi.class,ceshiAll);
workbook.write(new FileOutputStream(new File("E://easypoi.xlsx")));
}
工具類
/* //時間戳轉換成String日期 */
public static String stampToTime(long stamp) {
String sd = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sd = sdf.format(new Date(stamp)); // 時間戳轉換日期
return sd;
}
實體類
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Table(name = "ceshi")
public class Ceshi {
@Id
@Excel(name = "主鍵")
private Integer id;
@Excel(name = "姓名")
private String name;
@Excel(name = "密碼")
private String password;
// @Excel(name = "創建時間",exportFormat="yyyy-MM-dd HH:mm:ss",importFormat = "yyyy-MM-dd HH:mm:ss",width = 30)
@Excel(name = "創建時間")
private String updateTime;
}
運行後 打開.xlsx文件
下面粘貼一下企業級導出數據代碼應用示例
@Controller
@RequestMapping("/ceshi")
public class SysCeshiController {
@Autowired
private SysCeshiService sysCeshiService;
@RequestMapping("/download")
public void download(HttpServletResponse response) throws IOException {
//存儲的本地磁盤的路徑 在已創建的文件夾中創建文件
List<Ceshi> ceshiAll = sysCeshiService.getCeshiAll();
ceshiAll.forEach(e->e.setUpdateTime(FormatDateUtil.stampToTime(Long.parseLong(e.getUpdateTime()))));
//標題 表名 導出類型 HSSF xls XSSF xlsx
ExportParams exportParams = new ExportParams("測試表","ceshi", ExcelType.XSSF);
//1.導出參數對象 1.普通Java類的類對象(要導出的實體類的類對象) 3.一個集合 查詢全部的用戶信息
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Ceshi.class,ceshiAll);
/*String fileName="ceshi.xlsx";
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream;charset=UTF-8");
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); // 爲了解決中文名稱亂碼問題
response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
workbook.write(response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();*/
String path="E://";
String fileName="ceshi.xlsx";
File filePath = new File(path);
if (!filePath.exists()){
filePath.mkdirs();
}
try {
File file = new File(filePath, fileName);
FileOutputStream fos = new FileOutputStream(file);
workbook.write(fos);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
工作中可能還會遇到其他的業務,比如說導出圖片。這裏我還沒有遇到該業務,只能遇到時候在解決。今天就先寫到這裏。
這輩子堅持與不堅持都不可怕,怕的是獨自走在堅持的道路上!