import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
//import java.util.Random;
import javax.servlet.http.HttpSession;
import jxl.Cell;
import jxl.CellView;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.read.biff.BiffException;
import jxl.write.Label;
//import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Element;
import com.aspire.reportSystem.common.constant.LogConstant;
import com.aspire.reportSystem.common.dao.DBdao;
import com.aspire.reportSystem.common.exception.FrameException;
import com.aspire.reportSystem.common.log.LogWriter;
import com.aspire.reportSystem.common.log.ReportLog;
//import com.aspire.reportSystem.common.util.ServerDetector;
import com.aspire.reportSystem.common.util.ServerUtility;
import com.aspire.reportSystem.common.util.StringUtil;
import com.aspire.reportSystem.common.util.TimeUtil;
import com.aspire.reportSystem.common.util.XMLUtility;
import com.aspire.reportSystem.common.util.ZipUtils;
import com.aspire.reportSystem.portal.report.constant.Constant;
import com.aspire.reportSystem.portal.report.constant.ReportConstant;
import com.aspire.reportSystem.portal.report.dao.ReportDao;
import com.aspire.reportSystem.portal.report.exportReport.dao.ExcelDao;
import com.aspire.reportSystem.portal.report.exportReport.vo.ExcelReportDisplayVO;
import com.aspire.reportSystem.portal.report.sql.SQLAssembled;
import com.aspire.reportSystem.portal.report.util.ExcelFormatUtil;
import com.aspire.reportSystem.portal.report.util.OperateXML;
import com.aspire.reportSystem.portal.report.vo.ColumnFormatVO;
import com.aspire.reportSystem.portal.report.vo.ContentFormatVO;
import com.aspire.reportSystem.portal.report.vo.ExportFileVO;
import com.aspire.reportSystem.portal.report.vo.ReportConfigVO;
import com.aspire.reportSystem.portal.report.vo.ReportPageVO;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
* @author Lee
* @version 1.0
*/
public class ReportEngine4Excel {
private int newColumn;
private ExcelReportDisplayVO reportDisplayVO;
private ReportConfigVO reportConfigVO;
private ReportPageVO reportPageVO;
private List result = null;
private HttpSession session = null;
public static final int EXCEL_EXPORT_ERROR = 11;
public ReportEngine4Excel(HttpSession session,ReportPageVO reportPageVO,ReportConfigVO reportConfigVO) {
this.reportConfigVO = reportConfigVO;
this.reportPageVO = reportPageVO;
this.session = session;
restoreToReportPageVO();
}
private void excuteExcelSQL(String sql){
ReportDao reportDAO = new ReportDao();
result = reportDAO.getResultList(sql, null, 0, 0);
// /************************* for test start *************************************/
//
// result = new ArrayList();
//
// for(int i=0;i<32201;i++) {
// result.add(new String[]{"111","222","333"});
// }
/******************************* for test end **********************************/
ReportLog.sql(sql);
ReportLog.info("result.size() ::::::::::" + result.size());
}
/**
* 导出csv 格式的文件
* @param queryPageVO
* @param sql
* @param excelDao
* @param exportFileVO
* @return
* @throws Exception
*/
private String exportCSVFile(ReportPageVO queryPageVO,String sql,
ExcelDao excelDao,ExportFileVO exportFileVO) throws Exception{
int fileNum = 0;
int maxNum = 0;
int minNum = 0;
int maxRecordOnePage = exportFileVO.getMaxRecordOnePage();
int totalNum = exportFileVO.getTotalNum();
boolean isMod0 = false;
if(totalNum % maxRecordOnePage != 0) {
fileNum = totalNum/maxRecordOnePage + 1;
} else {
isMod0 = true;
fileNum = totalNum/maxRecordOnePage;
}
String tempFileFolderName = ServerUtility.getWeblogicPath()
+ File.separator+ReportConstant.TEMP_FOLDER_NAME;
File tempFolder = new File(tempFileFolderName);
if(!tempFolder.exists()) {
tempFolder.mkdir();
}
tempFileFolderName = tempFileFolderName + File.separator
+ "ep" + TimeUtil.getCurrentlyTimeByMillSecond();
File tempFileFolder = new File(tempFileFolderName);
if(!tempFileFolder.exists()) {
tempFileFolder.mkdir();
}
String tempFileZipName = tempFileFolderName + File.separator
+ queryPageVO.getTitle() + TimeUtil.getCurrentlyTime()+ ".zip";
OutputStream os=null;
List list = new ArrayList();
for(int i = 0;i<fileNum;i++) {
String tempFileName = tempFileFolderName + File.separator
+ queryPageVO.getTitle() + "_"
+ (i + 1) +"." + exportFileVO.getExtendName();
// tempFileName = new String(tempFileName.getBytes("ISO8859-1"),"GBK");
list.add(tempFileName);
File tempFile = new File(tempFileName);
try {
os = new FileOutputStream(tempFile);
maxNum = maxRecordOnePage * (i + 1);
minNum = maxRecordOnePage * i ;
if( i == fileNum - 1 && !isMod0) {
minNum = maxRecordOnePage * i;
maxNum = minNum + totalNum % maxRecordOnePage;
}
getHeadTitleString(queryPageVO,os);
getTitleCSVString(queryPageVO,os);
// if( ServerDetector.getServerId().equals("weblogic") ) {
excelDao.queryTable(sql, maxNum, minNum,os);
// } else {
// excelDao.queryTableForTomcat(sql, maxNum, minNum,os);
// }
} catch (IOException ex) {
ex.printStackTrace();
LogWriter.appendLog(this.getClass().getName(),LogConstant.LOG_LEVEL_ERROR, ex);
throw new FrameException(EXCEL_EXPORT_ERROR, "Excel文件操作错误" + ex.getMessage());
} finally{
if(os!= null) {
try {
os.flush();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
ZipUtils.gerZipFilesBySpeed(list, tempFileZipName,exportFileVO.getZipCompressLevel());
return tempFileZipName;
}
private void getHeadTitleString(ReportPageVO queryPageVO,OutputStream os) throws IOException{
ExcelReportDisplayVO rdVO = this.getReportDisplayVO();
os.write((queryPageVO.getTitle()+"/n").getBytes());
os.write((rdVO.getFromTime()+"/n").getBytes());
os.write((rdVO.getCreateTime()+"/n").getBytes());
os.write((rdVO.getQueryTerm()+"/n").getBytes());
}
/**
* 将 报表文件打成zip包,并导出
* @param out
* @param inputZipFile
*/
private void exportZipFile(OutputStream out,String inputZipFile) {
FileInputStream in = null;
try {
in = new FileInputStream(inputZipFile); // 读入文件
out.flush();
int aRead = 0;
while ((aRead = in.read()) != -1 & in != null) {
out.write(aRead);
}
out.flush();
} catch (Throwable e) {
// log.error("FileDownload doGet() IO error!",e);
} finally {
try {
in.close();
out.close();
} catch (Throwable e) {
// log.error("FileDownload doGet() IO close error!",e);
}
}
}
/**
* 画 csv文件剃头
* @param queryPageVO
* @param os
* @throws IOException
*/
private void getTitleCSVString(ReportPageVO queryPageVO,OutputStream os) throws IOException {
//画报表 列头 !!!!!!
List titleList = queryPageVO.getPageLayoutHeadList();
for(int i = 0; i <titleList.size()-1;i++) {
os.write((((ColumnFormatVO)titleList.get(i)).getCloumnName() +",").getBytes());
}
os.write((((ColumnFormatVO)titleList.get(titleList.size()-1)).getCloumnName()+"/n").getBytes());
}
public void restoreToReportPageVO() {
String pageLayoutKey = reportPageVO.getPageLayoutKey();
String sqlConfigKey = reportPageVO.getSqlConfigKey();
String roleright = reportPageVO.getRoleRight();
List nodeList = null;
Element pageLayout = null;
if (sqlConfigKey==null||"".equalsIgnoreCase(sqlConfigKey)){
<