導出excel簡單demo

public static void main(String[] args) throws Exception {
List<Map<String, Object>> listExcel = new ArrayList<Map<String,Object>>();//存儲excel導出數據
Map<String, Object> maps = new HashMap<String, Object>();//excel賦值
maps.put(“title”, “excel標題名”);
maps.put(“confidence”, “列名1”);
maps.put(“rate”, “列名2”);
maps.put(“review”, “列名3”);
maps.put(“reviewLength”, “列名4”);
maps.put(“null1”, “”);
maps.put(“null2”, “”);
maps.put(“null3”, “”);
maps.put(“null4”, “”);
maps.put(“null5”, “”);
maps.put(“accept”,“列名N”);
listExcel.add(maps);
exportExcel(listExcel);
}
public static void exportExcel(List<Map<String, Object>> listExcel) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(“工作表1”);
for(int j=0;j<=listExcel.size();j++){
HSSFRow row = sheet.createRow((int) 0);
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 創建一個居中格式
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 12);
style.setFont(font);
sheet.setColumnWidth((short) 0, (short) 2000);// 設置列寬
sheet.setColumnWidth((short) 1, (short) 5000);
sheet.setColumnWidth((short) 2, (short) 5000);
sheet.setColumnWidth((short) 3, (short) 5000);
sheet.setColumnWidth((short) 4, (short) 5000);
sheet.setColumnWidth((short) 5, (short) 5000);
sheet.setColumnWidth((short) 6, (short) 5000);
sheet.setColumnWidth((short) 7, (short) 5000);
sheet.setColumnWidth((short) 8, (short) 5000);
sheet.setColumnWidth((short) 9, (short) 5000);
sheet.setColumnWidth((short) 10, (short) 5000);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue(“序號”);
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue(“title”);
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue(“confidence”);
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue(“rate”);
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue(“review”);
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue(“reviewLength”);
cell.setCellStyle(style);
cell = row.createCell((short) 6);
cell.setCellValue(“null1”);
cell.setCellStyle(style);
cell = row.createCell((short) 7);
cell.setCellValue(“null2”);
cell.setCellStyle(style);
cell = row.createCell((short) 8);
cell.setCellValue(“null3”);
cell.setCellStyle(style);
cell = row.createCell((short) 9);
cell.setCellValue(“null4”);
cell.setCellStyle(style);
cell = row.createCell((short) 10);
cell.setCellValue(“null5”);
cell.setCellStyle(style);
cell = row.createCell((short) 11);
cell.setCellValue(“accept”);
cell.setCellStyle(style);
int p = listExcel.size()>65535?65535:listExcel.size();
int size = (§(j+1))>listExcel.size()?listExcel.size()😦§(j+1));
int index = j*65535;
HSSFCellStyle styleCell = wb.createCellStyle();
styleCell.setWrapText(true);
int count = 0;
// FORTEST
for (int i = index; i < size ; i++) {
if(count>=65535){
break;
}
count++;
row = sheet.createRow((int) i-index + 1);
Map<String, Object> map = listExcel.get(i);

			row.createCell((short) 0).setCellValue(i + 1);
			row.createCell((short) 1).setCellValue(checkMap(map.get("title")));
			row.createCell((short) 2).setCellValue(checkMap(map.get("confidence")));
			row.createCell((short) 3).setCellValue(checkMap(map.get("rate")));
			row.createCell((short) 4).setCellValue(checkMap(map.get("review")));
			row.createCell((short) 5).setCellValue(checkMap(map.get("reviewLength")));
			row.createCell((short) 6).setCellValue(checkMap(map.get("null1")));
			row.createCell((short) 7).setCellValue(checkMap(map.get("null2")));
			row.createCell((short) 8).setCellValue(checkMap(map.get("null3")));
			row.createCell((short) 9).setCellValue(checkMap(map.get("null4")));
			row.createCell((short) 10).setCellValue(checkMap(map.get("null5")));
			row.createCell((short) 11).setCellValue(checkMap(map.get("accept")));

		}
	}
	OutputStream out =  new FileOutputStream("D:\\export\\excel\\導出excel名稱.xlsx");
	try {
		wb.write(out);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
public static String checkMap(Object obj) {
	String str = obj + "";
	if (str.equals("null") || str.equals("")) {
		return null;
	} else {
		return str;
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章