pom.xml引入包:
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
代碼:
/**
* 導入Excel數據
*/
@PostMapping("import")
public AjaxResult importTradLine(@RequestParam(value = "file", required = false)MultipartFile file,@RequestParam String userId){
int b=0;
Date date = new Date();
DateFormat df = new SimpleDateFormat("hh:mm:ss");
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
// 1.獲取用戶上傳的文件
Workbook workbook = Workbook.getWorkbook(file.getInputStream());
if (workbook == null) {
return error("導入Excel爲空");
}
// 2.獲取工作簿sheet
Sheet sheet = workbook.getSheet(0);
// 3.獲取總行數
int rows = sheet.getRows();
System.out.println("rows:" + rows);
for (int i = 1; i < rows; i++) {
// Students students = new Students();
//Cell oCell= oFirstSheet.getCell(j,i);//需要注意的是這裏的getCell方法的參數,第一個是指定第幾列,第二個參數纔是指定第幾行
TradeLine tradeLine= new TradeLine();
String company2 = sheet.getCell(1, i).getContents();
String c1 =company2.replaceAll("\\s", "");
String c2 =company.replaceAll("\\s", "");
if (!c1.equals(c2)) {
continue;
}
tradeLine.setSupplier(c1);
tradeLine.setUnloadCompany(sheet.getCell(2, i).getContents());
tradeLine.setGoods(sheet.getCell(3, i).getContents());
tradeLine.setCarNumber(sheet.getCell(4, i).getContents());
tradeLine.setUnloadNumber(sheet.getCell(5, i).getContents());
tradeLine.setPrice(Double.parseDouble(sheet.getCell(6,i).getContents()));
tradeLine.setTon(Double.parseDouble(sheet.getCell(7, i).getContents()));
tradeLine.setAmounts(Double.parseDouble(sheet.getCell(8,i).getContents()));
String unloadTime=sheet.getCell(9, i).getContents();
String begin = unloadTime + " " + df.format(date);
Date d = dfs.parse(begin);
tradeLine.setUnloadDate(d);
tradeLine.setCreateDate(new Date());
// 4.添加到數據庫中
int a =tradeLineService.insert(tradeLine);
b++;
}
// 5.關閉資源
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("共導入了" + b + "條");
return success("共成功執行了" + b + "條");
}
/**
* 下載導入數據模板
*/
@GetMapping("import/template")
public AjaxResult exportTemplate(HttpServletRequest request,HttpServletResponse response) throws Exception {
// 1.文件下載響應頭
response.setContentType("application/msexcel");
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("模板導出.xls", "UTF-8"));
response.setCharacterEncoding("utf-8");
// 2.響應到瀏覽器
WritableWorkbook workbook = Workbook.createWorkbook(response.getOutputStream());
/**
* 定義單元格樣式
*/
WritableFont wf_title = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.RED); // 定義格式 字體 下劃線 斜體 粗體 顏色
WritableFont wf_head = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK); // 定義格式 字體 下劃線 斜體 粗體 顏色
WritableFont wf_table = new WritableFont(WritableFont.ARIAL, 8,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK); // 定義格式 字體 下劃線 斜體 粗體 顏色
WritableCellFormat wcf_title = new WritableCellFormat(wf_title); // 單元格定義
wcf_title.setBackground(jxl.format.Colour.BLACK); // 設置單元格的背景顏色
wcf_title.setAlignment(jxl.format.Alignment.CENTRE); // 設置對齊方式
WritableCellFormat wcf_head = new WritableCellFormat(wf_head);
wcf_head.setBackground(jxl.format.Colour.BLACK);
wcf_head.setAlignment(jxl.format.Alignment.CENTRE);
WritableCellFormat wcf_table = new WritableCellFormat(wf_table);
wcf_table.setBackground(jxl.format.Colour.BLACK);
wcf_table.setAlignment(jxl.format.Alignment.CENTRE);
// 創建工作簿sheet
WritableSheet sheet = workbook.createSheet("模板", 0);
// 3.設置column名
sheet.addCell(new Label(0, 0, "序號",wcf_head));
sheet.addCell(new Label(1, 0, "供應單位",wcf_head));
sheet.addCell(new Label(2, 0, "卸貨廠家",wcf_head));
sheet.addCell(new Label(3, 0, "貨品種類",wcf_head));
sheet.addCell(new Label(4, 0, "車牌號碼",wcf_head));
sheet.addCell(new Label(5, 0, "卸貨磅單",wcf_head));
sheet.addCell(new Label(6, 0, "貨品單價(元/噸)",wcf_head));
sheet.addCell(new Label(7, 0, "卸貨噸數",wcf_head));
sheet.addCell(new Label(8, 0, "總金額(元)",wcf_head));
sheet.addCell(new Label(9, 0, "卸貨日期",wcf_head));
// 4.把覈保的數據填充到工作簿中 service調用selectExport()查詢數據庫
System.out.println("開始導出...");
long s1 = System.currentTimeMillis();
// 5.寫入數據
workbook.write();
// 6.關閉資源
workbook.close();
long s2 = System.currentTimeMillis();
long time = s2 - s1;
System.out.println("導出完成!消耗時間:" + time + "毫秒");
return null;
}
備註:如果是查詢數據庫數據,導出數據,在導出模板代碼塊中,加入個循環插入即可,不再重複寫代碼了。