数据库文件导出

 

 

 //==============================================核心类===============================================================

public static ByteArrayInputStream genExcel(Map<String,Object> excelMap)
throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("sheet1");

List<String> header = (List<String>)excelMap.get("header");
List<List<Object>> contents = (List<List<Object>>)excelMap.get("contents");

int ri = 0, ci = 0;
HSSFRow row = sheet.createRow(ri);
HSSFCell cell = null;
//列名字体加粗
HSSFCellStyle cellStyle = workbook.createCellStyle();
HSSFFont cellFont = workbook.createFont();
cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellStyle.setFont(cellFont);
if (header != null && contents != null) {
//生成列名
for (ci = 0; ci < header.size(); ci++) {
cell = row.createCell(ci);
cell.setCellValue(header.get(ci));
cell.setCellStyle(cellStyle);
}
//行数据生成
for (ri = 0; ri < contents.size(); ri++) {
row = sheet.createRow(ri + 1);
List<Object> data = contents.get(ri);
for (ci = 0; ci < data.size(); ci++) {
cell = row.createCell(ci);
Object value = data.get(ci);

if(value != null){
if (value instanceof Long || value instanceof Integer ||
value instanceof Short || value instanceof Byte ||
value instanceof AtomicInteger ||
value instanceof AtomicLong ||
value instanceof BigInteger ||
value instanceof BigDecimal ||
value instanceof Float ||
value instanceof Double) {
cell.setCellValue(((Number) value).doubleValue());
}else if(value instanceof Boolean){
cell.setCellValue((Boolean) value);
}else if(value instanceof byte[]){
cell.setCellValue(DataFormat.bytes2HexString((byte[])value));
}else{
cell.setCellValue(String.valueOf(value));
}
}
}
}
}

ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.flush();
workbook.write(baos);
byte[] ba = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(ba);

return bais;
}
}

posted @ 2017-08-21 15:19  贪吃的柳柳  阅读(122)  评论(0编辑  收藏  举报