java控制excel_java操作excel
1. 生成并下載excel文件
controller
@RequestMapping(value = "/download", method = RequestMethod.GET)
public xxx downloadFile(HttpServletResponse response) {
response.setContentType("application/octet-stream");
response.setHeader("content-type", "application/octet-stream");
try {
response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(fileName, "UTF-8"));// 設置文件名
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
excelService.download(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
service
public interface ExcelService {
void download(OutputStream outputStream) throws IOException;
}
impl
@Override
public void download(OutputStream outputStream) throws IOException{
//1.創建excel(工作簿)
Workbook wb = new XSSFWorkbook();
//2.創建Sheet
Sheet sheet = wb.createSheet();
//3.創建表頭,參數為要創建的行的位置
Row header = sheet.createRow(0);
//4.創建單元格對象,向單元格寫數據
Cell cell = null;
List headerList = Lists.newArrayList("姓名","年齡","性別","愛好","生日");
for (int i = 0; i < headerList.size(); i++) {
//行對象.createCell(單元格位置)
cell = header.createCell(i);
//賦值
cell.setCellValue(headerList.get(i));
}
//5.工作簿寫進輸出流
wb.write(outputStream);
}
2. 解析前端上傳的excel
Controller
@RequestMapping(value = "/excel/import", method = RequestMethod.GET)
public xxx uploadExcel(@RequestParam("upload") MultipartFile file) {
excelService.uploadExcel(file);
return xxx.OK;
}
service
public interface ExcelService {
void uploadExcel(MultipartFile multipartFile);
}
impl
public void uploadExcel(MultipartFile multipartFile) {
List dataList = ExcelUtil.loadExcelData(multipartFile);
}
ExcelUtil
public static List loadExcelData(MultipartFile file) throws Exception {
Workbook wb;// 創建Excel2003文件對象
wb = WorkbookFactory.create(file.getInputStream());
//獲取第一個頁簽對象
Sheet sheet = wb.getSheetAt(0);
//獲取有內容的總行數
int rowsNumber = sheet.getLastRowNum();
if (rowsNumber >= 0) {
List excelList = new ArrayList<>(rowsNumber);
//遍歷頁簽的每一行
Row firstRow = sheet.getRow(0);
if (rowsNumber == 0 && firstRow == null) {
return null;
}
//總列數
int cellsNumber = sheet.getRow(0).getLastCellNum();
for (int i = 0; i <= rowsNumber; i++) {
Row row = sheet.getRow(i);// 獲取行對象
if (row == null) {// 如果為空,不處理
continue;
}
int num = 0;//記錄該行的空格總數
List newList = new ArrayList<>(cellsNumber);
for (int j = 0; j < cellsNumber; j++) {
Cell cell = row.getCell(j);// 獲取單元格對象
if (cell != null) {
String value = getValueByType(cell);
if (StringUtils.isEmpty(value)) {
num += 1;
}
newList.add(value);
} else {
newList.add("");
num += 1;
}
}
if (num < cellsNumber) {//不是全部空格,把這行加到返回數據中
excelList.add(newList);
}
}
return excelList;
}
wb.close();
return null;
}
private static String getValueByType(Cell cell) {
//判斷是否為null或空串
if (cell==null || cell.toString().trim().equals("")) {
return "";
}
String cellValue = "";
int cellType=cell.getCellType();
if(cellType==Cell.CELL_TYPE_FORMULA){ //表達式類型
cellType=evaluator.evaluate(cell).getCellType();
}
switch (cellType) {
case Cell.CELL_TYPE_STRING: //字符串類型
cellValue= cell.getStringCellValue().trim();
cellValue=StringUtils.isEmpty(cellValue) ? "" : cellValue;
break;
case Cell.CELL_TYPE_BOOLEAN: //布爾類型
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC: //數值類型
if (HSSFDateUtil.isCellDateFormatted(cell)) { //判斷日期類型
cellValue = DateUtil.formatDateByFormat(cell.getDateCellValue(), "yyyy-MM-dd");
} else { //否
cellValue = new DecimalFormat("#.######").format(cell.getNumericCellValue());
}
break;
default: //其它類型,取空串吧
cellValue = "";
break;
}
return cellValue;
}
總結
以上是生活随笔為你收集整理的java控制excel_java操作excel的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: gps卫星位置计算程序matlab_科研
- 下一篇: php 5.6 文档,文件存储 | 进阶
