first commit
This commit is contained in:
97
hrmsEjb/wenrgise/report/excel/ExcelReport.java
Normal file
97
hrmsEjb/wenrgise/report/excel/ExcelReport.java
Normal file
@@ -0,0 +1,97 @@
|
||||
package wenrgise.report.excel;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.Locale;
|
||||
import javax.sql.DataSource;
|
||||
import jxl.WorkbookSettings;
|
||||
import jxl.format.CellFormat;
|
||||
import jxl.write.Label;
|
||||
import jxl.write.Number;
|
||||
import jxl.write.NumberFormats;
|
||||
import jxl.write.WritableCell;
|
||||
import jxl.write.WritableCellFormat;
|
||||
import jxl.write.WritableFont;
|
||||
import jxl.write.WritableSheet;
|
||||
import wenrgise.common.utility.ServiceLocator;
|
||||
|
||||
class ExcelReport {
|
||||
WorkbookSettings ws = null;
|
||||
|
||||
WritableSheet s = null;
|
||||
|
||||
public ExcelReport() {
|
||||
this.ws = new WorkbookSettings();
|
||||
this.ws.setLocale(new Locale("en", "EN"));
|
||||
}
|
||||
|
||||
public WorkbookSettings getWorkBookSetting() {
|
||||
return this.ws;
|
||||
}
|
||||
|
||||
public void writeDataSheet(WritableSheet s, ArrayList heading, String query) {
|
||||
DataSource oDataSource = null;
|
||||
Statement stmt = null;
|
||||
Connection conn = null;
|
||||
ResultSet results = null;
|
||||
DecimalFormat Currency = new DecimalFormat("0.00");
|
||||
try {
|
||||
oDataSource = (DataSource)ServiceLocator.getLocator().getService("jdbc/conDS");
|
||||
conn = oDataSource.getConnection();
|
||||
stmt = conn.createStatement();
|
||||
results = stmt.executeQuery(query);
|
||||
int rowPos = 0;
|
||||
WritableFont wf = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
|
||||
WritableCellFormat cf = new WritableCellFormat(wf);
|
||||
cf.setWrap(false);
|
||||
int colPos = -1;
|
||||
Iterator itrHeading = heading.iterator();
|
||||
while (itrHeading.hasNext()) {
|
||||
colPos++;
|
||||
Heading oHeading = itrHeading.next();
|
||||
String sHeading = oHeading.getName();
|
||||
s.addCell((WritableCell)new Label(colPos, rowPos, sHeading, (CellFormat)cf));
|
||||
}
|
||||
wf = new WritableFont(WritableFont.ARIAL, 8, WritableFont.NO_BOLD);
|
||||
while (results.next()) {
|
||||
rowPos++;
|
||||
colPos = -1;
|
||||
Iterator itrHeading1 = heading.iterator();
|
||||
while (itrHeading1.hasNext()) {
|
||||
colPos++;
|
||||
Heading oHeading1 = itrHeading1.next();
|
||||
String sHeading1 = oHeading1.getName();
|
||||
int sType = oHeading1.getType();
|
||||
if (sType == 1) {
|
||||
cf = new WritableCellFormat(wf);
|
||||
cf.setWrap(false);
|
||||
s.addCell((WritableCell)new Label(colPos, rowPos, results.getString(sHeading1), (CellFormat)cf));
|
||||
continue;
|
||||
}
|
||||
if (sType == 2) {
|
||||
cf = new WritableCellFormat(wf, NumberFormats.FLOAT);
|
||||
cf.setWrap(false);
|
||||
s.addCell((WritableCell)new Number(colPos, rowPos, results.getBigDecimal(sHeading1).floatValue(), (CellFormat)cf));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (stmt != null)
|
||||
stmt.close();
|
||||
if (results != null)
|
||||
results.close();
|
||||
if (conn != null)
|
||||
conn.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
87
hrmsEjb/wenrgise/report/excel/ExcelReportInformation.java
Normal file
87
hrmsEjb/wenrgise/report/excel/ExcelReportInformation.java
Normal file
File diff suppressed because one or more lines are too long
63
hrmsEjb/wenrgise/report/excel/ExcelReportServlet.java
Normal file
63
hrmsEjb/wenrgise/report/excel/ExcelReportServlet.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package wenrgise.report.excel;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import jxl.Workbook;
|
||||
import jxl.WorkbookSettings;
|
||||
import jxl.write.WritableSheet;
|
||||
import jxl.write.WritableWorkbook;
|
||||
|
||||
public class ExcelReportServlet extends HttpServlet {
|
||||
private static final String CONTENT_TYPE_EXCEL = "application/vnd.ms-excel";
|
||||
|
||||
private static final String CONTENT_TYPE_ZIP = "application/zip";
|
||||
|
||||
public void init(ServletConfig config) throws ServletException {
|
||||
super.init(config);
|
||||
}
|
||||
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||
processRequest(request, response);
|
||||
}
|
||||
|
||||
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||
processRequest(request, response);
|
||||
}
|
||||
|
||||
private boolean isGzipSupported(HttpServletRequest request) {
|
||||
String encodings = request.getHeader("Accept-Encoding");
|
||||
return (encodings != null && encodings.indexOf("gzip") != -1);
|
||||
}
|
||||
|
||||
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||
response.setContentType("application/zip");
|
||||
String reportName = request.getParameter("reportname");
|
||||
String inputParam1 = request.getParameter("inputparam1");
|
||||
String inputParam2 = request.getParameter("inputparam2");
|
||||
ServletOutputStream servletOutputStream = response.getOutputStream();
|
||||
GZIPOutputStream gZIPOutputStream = new GZIPOutputStream((OutputStream)servletOutputStream);
|
||||
ExcelReport excelReport = new ExcelReport();
|
||||
WorkbookSettings ws = excelReport.getWorkBookSetting();
|
||||
try {
|
||||
WritableWorkbook workbook = Workbook.createWorkbook(gZIPOutputStream, ws);
|
||||
WritableSheet s = workbook.createSheet(reportName, 0);
|
||||
ExcelReportInformation ex = new ExcelReportInformation(reportName);
|
||||
String query = ex.getReportQuery(inputParam1, inputParam2);
|
||||
ArrayList aHeader = ex.getReportHeader();
|
||||
excelReport.writeDataSheet(s, aHeader, query);
|
||||
workbook.write();
|
||||
workbook.close();
|
||||
gZIPOutputStream.flush();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
20
hrmsEjb/wenrgise/report/excel/Heading.java
Normal file
20
hrmsEjb/wenrgise/report/excel/Heading.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package wenrgise.report.excel;
|
||||
|
||||
public class Heading {
|
||||
private String sName;
|
||||
|
||||
private int sType;
|
||||
|
||||
public Heading(String Name, int Type) {
|
||||
this.sName = Name;
|
||||
this.sType = Type;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.sName;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return this.sType;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user