package cc.dynasoft.struts.action;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
import cc.dynasoft.bean.Department;
public class OutputExcel {
public static boolean outputExcel(ExcelArgs args, List title, List list) {
try {
int cellNum = args.getCellNum();
int rowNum = args.getRowNum(); HSSFWorkbook wb =
new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(args.getSheetName()); HSSFPrintSetup hps = sheet.getPrintSetup(); hps.setPaperSize((
short)
9); sheet.setHorizontallyCenter(
true); wb.setPrintArea(
0,
"$A$2:$e$" + rowNum +
2); HSSFFooter footer = sheet.getFooter(); footer.setCenter(
"Time:" + HSSFFooter.date()); footer.setRight(
"Page " + HSSFFooter.page() +
" of " + HSSFFooter.numPages()); HSSFHeader header = sheet.getHeader(); header.setCenter(HSSFHeader.font(
"Stencil-Normal",
"Italic") + HSSFHeader.fontSize((
short)
30) + args.getHeaderTitle()); header.startUnderline(); sheet.setColumnWidth((
short)
2, (
short) ((
30 *
8) / ((
double)
1 /
10))); sheet.setColumnWidth((
short)
3, (
short) ((
40 *
8) / ((
double)
1 /
10))); sheet.setColumnWidth((
short)
4, (
short) ((
50 *
8) / ((
double)
1 /
20))); HSSFRow row0 = sheet.createRow(
0); row0.setHeight((
short)
0x300); HSSFFont font2 = wb.createFont(); font2.setColor(HSSFFont.SS_NONE); font2.setFontHeight((
short)
700); font2.setFontName(
"Courier New"); HSSFCell cell0 = row0.createCell((
short)
0); HSSFCellStyle style3 = wb.createCellStyle(); style3.setAlignment(HSSFCellStyle.VERTICAL_CENTER); style3.setAlignment(HSSFCellStyle.ALIGN_CENTER); style3.setFont(font2); cell0.setCellStyle(style3); cell0.setCellValue(args.getHeaderTitle()); sheet.addMergedRegion(
new Region(
0, (
short)
0,
0, (
short) (cellNum -
1))); HSSFCellStyle style = wb.createCellStyle(); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(HSSFColor.BLACK.index); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setLeftBorderColor(HSSFColor.BLACK.index); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setRightBorderColor(HSSFColor.BLACK.index); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setTopBorderColor(HSSFColor.BLACK.index); HSSFCellStyle style1 = wb.createCellStyle(); style1.setBorderBottom(HSSFCellStyle.BORDER_THIN); style1.setBottomBorderColor(HSSFColor.BLACK.index); style1.setBorderLeft(HSSFCellStyle.BORDER_THIN); style1.setLeftBorderColor(HSSFColor.BLACK.index); style1.setBorderRight(HSSFCellStyle.BORDER_THIN); style1.setRightBorderColor(HSSFColor.BLACK.index); style1.setBorderTop(HSSFCellStyle.BORDER_MEDIUM); style1.setTopBorderColor(HSSFColor.BLACK.index); style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style1.setFillForegroundColor(
new HSSFColor.RED().getIndex()); style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFFont font = wb.createFont(); font.setColor(HSSFFont.SS_NONE); font.setFontName(
"Courier New"); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); style1.setFont(font); HSSFRow row1 = sheet.createRow((
short)
1);
for (
int j =
0; j < cellNum; j++) { HSSFCell cell = row1.createCell((
short) j); cell.setCellValue((String) title.get(j)); cell.setCellStyle(style1); } Iterator iter = list.iterator();
for (
int i =
2; iter.hasNext(); i++) { Department dep = (Department) iter.next(); HSSFRow row = sheet.createRow((
short) i); HSSFCell cell5 = row.createCell((
short)
0); HSSFCell cell1 = row.createCell((
short)
1); HSSFCell cell2 = row.createCell((
short)
2); HSSFCell cell3 = row.createCell((
short)
3); HSSFCell cell4 = row.createCell((
short)
4); cell5.setCellValue(dep.getId()); cell5.setCellStyle(style); cell1.setCellValue(dep.getParentId()); cell1.setCellStyle(style); cell2.setCellValue(dep.getName()); cell2.setCellStyle(style); cell3.setCellValue(dep.getDescription()); cell3.setCellStyle(style); cell4.setCellValue(dep.getImagePath()); cell4.setCellStyle(style); } FileOutputStream fileOut =
new FileOutputStream(args .getPathAndName()); wb.write(fileOut); }
catch (IOException ex) { ex.printStackTrace(); }
catch (Exception ex) { ex.printStackTrace(); }
return true; }}