导出Excel工具类
import org.apache.poi.hssf.usermodel.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class ExcelExportUtil {
public static boolean createExcel(String filepath, String filename, List<String> titlelist, List<String> zdlist, List<Map<String,Object>> datalist) throws IOException{
boolean success = false;
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");
HSSFRow row0 = sheet.createRow(0);
for(int i = 0;i<titlelist.size();i++){
row0.createCell(i).setCellValue(titlelist.get(i));
}
for(int row = 0;row<datalist.size();row++){
HSSFRow newrow = sheet.createRow(row+1);
@SuppressWarnings("unchecked")
Map<String,Object> data = (Map<String, Object>) datalist.get(row);
for(int col = 0;col<zdlist.size();col++){
newrow.createCell(col).setCellValue(data!=null&&data.get(zdlist.get(col))!=null?String.valueOf(data.get(zdlist.get(col))):"");
}
}
isChartPathExist(filepath);
FileOutputStream output=new FileOutputStream(filepath+"/"+filename);
wb.write(output);
output.close();
success = true;
} catch (Exception e) {
success = false;
e.printStackTrace();
}
return success;
}
private static void isChartPathExist(String dirPath) {
File file = new File(dirPath);
if (!file.exists()) {
file.mkdirs();
}
}
}