`
chilongxph
  • 浏览: 135552 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

使用poi导出excel

阅读更多
private void writeBackupSoftExcel(OutputStream os,
TreeMap<String, ArrayList<String>> result,
ArrayList<Record> staffList) {
FileInputStream tp_in = null;
HSSFWorkbook workBook = null;
HSSFRow row = null;
HSSFCell cell = null;
// ArrayList al=new ArrayList();
// set red color style

workBook = new HSSFWorkbook();

// 创建样式一
HSSFCellStyle cs1 = workBook.createCellStyle();
HSSFFont font1 = workBook.createFont();
font1.setFontName("Arial");// 设置字体
font1.setFontHeightInPoints((short) 17);// 字体大小
font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体字体
cs1.setFont(font1);
cs1.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中
cs1.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 下边框
cs1.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
cs1.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
cs1.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
cs1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cs1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cs1.setWrapText(false);

// 创建样式二
HSSFCellStyle cs2 = workBook.createCellStyle();
HSSFFont font2 = workBook.createFont();
font2.setFontName("Arial");// 设置字体
font2.setFontHeightInPoints((short) 10);// 字体大小
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);// 正常字体
cs2.setFont(font2);
cs2.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 居左对齐
cs2.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 下边框
cs2.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
cs2.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
cs2.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
// cs1.setAlignment(HSSFCellStyle.ALIGN_CENTER);

cs2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cs2.setWrapText(false);



//设置单个sheet的列数
int colnum=Constants.COL_NUM;

//System.out.println("--------------创建多个sheet---------");
try {
// set the sheet name

for (int m = 0; m <= staffList.size() / colnum; m++) {
// HSSFSheet sheet1 = workBook.getSheetAt(0);
HSSFSheet sheet1 = workBook.createSheet();
sheet1.setColumnWidth((short) 0, (short) (36 * 360));
workBook.setSheetName(m, "Soft Info "+m);


row = sheet1.getRow(0);
//合并A到G列
sheet1.addMergedRegion(new Region(0, (short) 0, 0, (short) 6));
if (row == null)
row = sheet1.createRow(0);
// 设置行高
row.setHeight((short) 600);
// 设置标题名
cell = row.createCell((short) 0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(cs1);
cell.setCellValue("Detail    Soft    Info");


// sheet1.mergeCells( 0 , 0 , 5 , 0 );



row = sheet1.getRow(1);
if (row == null)
row = sheet1.createRow(1);
// HSSFCellStyle cs3 = workBook.createCellStyle();
// //设置行背景色
// cs3.setFillBackgroundColor(new
// HSSFColor.GREY_25_PERCENT().getIndex());
// cs3.setFillForegroundColor(HSSFColor.AQUA.index);
if (staffList.size() > (m + 1) * colnum) {

for (int i = m * colnum; i < (m + 1) * colnum; i++) {
int n = i % colnum + 1;
//设置列宽
sheet1.setColumnWidth((short)n,(short)(28*200));
// //自动调整列宽
// sheet1.autoSizeColumn((short)n);
cell = row.getCell((short) n);
// cell = row.createCell((short) 0);
// cell.setCellValue("SoftWare");
if (cell == null)
cell = row.createCell((short) n);

cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(cs2);
boolean isPubServer = staffList.get(i).getString(
"staffno").equals(
staffList.get(i).getString("staffname"));
if (!isPubServer) {
cell.setCellValue(staffList.get(i).getString(
"staffname")
+ "("
+ staffList.get(i).getString("staffno")
+ ")");
} else {
cell.setCellValue(staffList.get(i).getString(
"staffname")
+ "[Pub Server]");
}
}
} else {
for (int i = m * colnum; i < staffList.size(); i++) {
cell = row.getCell((short) (i + 1));
int n = i % colnum + 1;
if (cell == null)
cell = row.createCell((short) n);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(cs2);
//设置列宽
sheet1.setColumnWidth((short)n,(short)(28*200));
boolean isPubServer = staffList.get(i).getString(
"staffno").equals(
staffList.get(i).getString("staffname"));
if (!isPubServer)
cell.setCellValue(staffList.get(i).getString(
"staffname")
+ "("
+ staffList.get(i).getString("staffno")
+ ")");
else
cell.setCellValue(staffList.get(i).getString(
"staffname")
+ "[Pub Server]");
}
}
// write the result
String key;
String soft_name;
String staff_id;
String vaild;
Iterator<String> it = result.keySet().iterator();
ArrayList<String> staffIDList;
for (int i = 0; i < result.size(); i++) {
key = it.next();
vaild = key.substring(0, 1);
soft_name = key.substring(1);
// start from 3st row
row = sheet1.getRow(i + 2);
if (row == null) {
row = sheet1.createRow(i + 2);
}
// write first field for soft name
cell = row.getCell((short) 0);
if (cell == null) {
cell = row.createCell((short) 0);
}
// create a font style
HSSFCellStyle style = workBook.createCellStyle();
HSSFFont redFont = workBook.createFont();
redFont.setColor(HSSFFont.COLOR_RED);
HSSFFont blackFont = workBook.createFont();
blackFont.setColor(HSSFFont.COLOR_NORMAL);
// judge illegal soft
if (vaild.equals("0")) {
style.setFont(redFont);
cell.setCellStyle(style);
}
cell.setCellValue(soft_name);
// write "*"

if ((m + 1) * colnum < staffList.size()) {
staffIDList = result.get(key);
// 获取staffId
for (int j = 0; j < staffIDList.size(); j++) {
staff_id = staffIDList.get(j);

for (int k = m * colnum; k < (m + 1) * colnum; k++) {
if (staff_id.equals(staffList.get(k).getString(
"staff_id"))) {
int n = k % colnum + 1;
cell = row.getCell((short) (n));
if (cell == null)
cell = row.createCell((short) (n));
cell.setCellValue("*");
break;
}
}
}
} else {
staffIDList = result.get(key);
for (int j = 0; j < staffIDList.size(); j++) {
staff_id = staffIDList.get(j);
for (int k = m * colnum; k < staffList.size(); k++) {
if (staff_id.equals(staffList.get(k).getString(
"staff_id"))) {
int n = k % colnum + 1;
cell = row.getCell((short) n);
if (cell == null)
cell = row.createCell((short) n);
cell.setCellValue("*");
break;
}
}
}
}
}
}
workBook.write(os);
// workBook.write(os);
} catch (Exception e) {
e.printStackTrace();
setErrorMsg("Write excel backup file error!");
log.error(getErrorMsg() + e);
throw new BizException(e);
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (tp_in != null) {
try {
tp_in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}



使用这种方式可以有效的避免使用模板所带来的性能问题和jvm内存不足的问题,可是具体怎么做到对样式的灵活控制,我还没有搞的太明白,如有大侠路过,还请不吝赐教。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics