Today I would like to tidy up some of the problems I encountered in the previous period of export.
Because of the requirements of the project, to do a part of the export function. At the beginning of the company's export, but it is very strange that some of the modules are exported when the 500 error, found that the deletion of some fields after the return to normal, at that time because of the project tension, but also temporarily cut some, but also is not a long-term, after their original on the basis of the revision of the finishing up a bit, The current operation is still stable, and share with you about this.
Export requires three parts, JS, public methods, background methods.
JS Code
function ExportData () {//foreground received parameter RWMC = $ ("#txt_rwmc"). Val ();
RWLB = $ ("#com_rwlb"). ComboBox ("GetValues"). Join (","); Invoke the background export feature var service = new Service (CX.
Rybjcxbndservice.exprotexcel ");
var str= "<RWMC>" + RWMC + "</RWMC><RWLB>" + rwlb + "</RWLB>";
var res = service.doservice (str);
var odoc = Loadxml (res);
if (Service.getcode ()!= "") {showmessage ("Query failed:" +service.getmessage ());
Return
var nodata = Odoc.selectsinglenode ("Root/nodata"). Text;
if (NoData = = "NoData") {showmessage ("no data!");
Return
//Get export information var titlename = Odoc.selectsinglenode ("Root/title_name"). Text;
var fileName = Odoc.selectsinglenode ("Root/file_name"). Text;
var outputinfo = Odoc.selectsinglenode ("Root/outputinfo"). Text;
var Download_path = Odoc.selectsinglenode ("Root/download_path"). Text;
if (Outputinfo!= "") {ShowMessage (outputinfo);
Return The IF (confirm) ("Export succeeded!") confirm the download file? \ nthe filename is called: "+filename)" {var file= FileName;
var showfile = TitleName + ". xls";
Showfile = decodeURIComponent (showfile); var idx = document.
Url.indexof ("/adp"); if (idx = = 1) {Alert ("host address not recognized:" + document.
URL);
Return The var host = document.
Url.substring (0, IDX);
var width = screen.width;
var height = screen.height;
Debugger Open download page var param = "toolbar=no,location=no,status=yes,resizable=no,scrollbars=yes,top=" + height + ", left=" + width
+ ", width=100,height=100"; ----------------------------------------------------------//This code block exports Excel failure problem to resolve IE6, because IE6 to window.open (URL) Poor support,///When the browser is IE6, use window.location.href var isie=!! instead Window.
ActiveXObject; var Isie6=isie&&!window.
XMLHttpRequest; if (isIE6) {window.location.href=host + "/adp/work/gzkp/common/js/download_new.jsp?file=" + file + "&showfile="
+ Showfile + "&download_path=" + download_path; else {window.open (host + "/adp/work/gzkp/common/js/downLoad_new.jsp?file= "+ file +" &showfile= "+ showfile +" &download_path= "+ download_path," _blank ", param); }
}
}
public class
Package Ctais.business.gzkp.common;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Org.apache.poi2.hssf.usermodel.HSSFCell;
Import Org.apache.poi2.hssf.usermodel.HSSFCellStyle;
Import Org.apache.poi2.hssf.usermodel.HSSFFont;
Import Org.apache.poi2.hssf.usermodel.HSSFRow;
Import Org.apache.poi2.hssf.usermodel.HSSFSheet;
Import Org.apache.poi2.hssf.usermodel.HSSFWorkbook;
Import Ctais.business.dashboard.service.ExportExcel;
Import Ctais.config.Config;
Import Ctais.services.data.DataWindow;
Import Ctais.services.xml.XMLDataObject;
Import Ctais.services.xml.XMLParser;
Import ctais.util.StringEx; Import JXL.
workbook;
Import jxl.format.Alignment;
Import Jxl.write.Label;
Import Jxl.write.WritableCellFormat;
Import Jxl.write.WritableFont;
Import Jxl.write.WritableSheet;
Import Jxl.write.WritableWorkbook; /** * <p>title: Generate Excel File </p> * <p>description: Convert string string to Excel document </p> * <p>copyrIght:copyright (c) 2004</p> * <p>Company:DC</p> * @author FENGZG * @version 1.0 * Time: 2015-12-28 *
/public class Createexcel {private final static String Config_file_path = Config.ctais_home;
Writableworkbook WWB = null;
Xmldataobject xdo = null; Public Createexcel () {}/** * generates EXCEL * @param SQL query SQL * @param CZRYDM operator Code * @param Titles Export column headings * @param exltitle Excel Header * @return * @throws Exception/public String Newtoexcel ( String sql,string czrydm,string[] titles,string exltitle) throws Exception {try {Hssfworkbook wb = new
Hssfworkbook ();
Hssfsheet sheet = Wb.createsheet ();
Exportexcel exportexcel = new Exportexcel (WB, sheet);
StringBuffer sffer = new StringBuffer ();
int colnum = 30;
DataWindow DW = Datawindow.dynamiccreate (sql.tostring ());
Dw.setconnectionname (Icomm.gzkpjndi); Long Dwret = DW.Retrieve ();
if (dwret <= 0) {sffer.append ("<NODATA>nodata</NODATA>");
return sffer.tostring ();
else {sffer.append ("<NODATA></NODATA>");
int colnum = Dw.getcolumncount ();
Define column widths for worksheet columns (actually apply the number of changes to the columns) for (short i = 0; I <= colnum; i++) {Sheet.setcolumnwidth (I, (short) 4000);
//Create cell style Hssfcellstyle Cellheadstyle = Wb.createcellstyle ();
Specifies the center-aligned Cellheadstyle.setalignment (hssfcellstyle.align_center) of cells;
Specifies that the cell is centered vertically cellheadstyle.setverticalalignment (hssfcellstyle.vertical_center);
Specifies that line Cellheadstyle.setwraptext (true) is automatically wrapped when the cell contents are not displayed;
Set cell font Hssffont Headfont = Wb.createfont ();
Headfont.setboldweight (Hssffont.boldweight_bold);
Headfont.setfontname ("Song Body");
Headfont.setfontheight ((short) 200);
Cellheadstyle.setfont (Headfont);
Create Report Header date Dt=new date ();
SimpleDateFormat sdt=new SimpleDateFormat ("yyyymmddhhmmsss");
String SFM = CZRYDM + "_" + sdt.format (DT);
Set column header Exportexcel.createnormalhead (Exltitle, colNum-1);
Hssfrow row1 = Sheet.createrow (1);
for (int i = 0; i < titles.length i + +) {Hssfcell cell = Row1.createcell ((short) i);
Cell.setencoding (HSSFCELL.ENCODING_UTF_16);
Cell.setcellstyle (Cellheadstyle);
Cell.setcellvalue (Titles[i]);
} Object value = "";
Set Table Style Hssfcellstyle CellStyle = Wb.createcellstyle ();
Specifies the center-aligned Cellstyle.setalignment (hssfcellstyle.align_center) of cells;
Specifies that the cell is centered vertically cellstyle.setverticalalignment (hssfcellstyle.vertical_center);
Specifies that line Cellstyle.setwraptext (true) is automatically wrapped when the cell contents are not displayed;
Set cell font Hssffont font = Wb.createfont (); Font.setboldweight (HSSffont.ss_none);
Font.setfontname ("Song Body");
Font.setfontheight ((short) 200);
Cellstyle.setfont (font);
for (int i = 0; i < Dw.getrowcount (); i++) {Hssfrow row = Sheet.createrow (i + 2);
for (int j = 1; J <= Dw.getcolumncount (); j + +) {Hssfcell cell = Row.createcell (short) (j-1));
Cell.setencoding (HSSFCELL.ENCODING_UTF_16);
Cell.setcellstyle (CellStyle);
Value = Dw.getitemany (i, j-1);
if (value = = null) {Cell.setcellvalue ("");
else {Cell.setcellvalue (value.tostring ()); }}//Set the export path, where you need to be aware that if the Linux system needs to manually build the path, (please advise if the reason is clear) refer to the new file path String path = "/expo
rt/";
File File = new file (path);
if (!file.exists ()) {file.mkdirs ();
String fileName = sfm+ ". xls";
String pth = Path.trim () + File.separator + fileName; String Pth = Path.trim () + fileName;
PTH = Pth.trim ();
String Outputinfo = Exportexcel.outputexcel (PTH);
Sffer.append ("<TITLE_NAME>" + SFM + "</TITLE_NAME>");
Sffer.append ("<DOWNLOAD_PATH>" + PATH + "</DOWNLOAD_PATH>");
Sffer.append ("<FILE_NAME>" + fileName + "</FILE_NAME>");
Sffer.append ("<OUTPUTINFO>" + outputinfo + "</OUTPUTINFO>");
return sffer.tostring ();
catch (Exception e) {e.printstacktrace ();
throw new Exception (E.getmessage ()); }
}
}
Background Code
/** * Export function * @param xdo foreground reference * @param CZRYDM operator code * @return generated XLS information * @throws Exception Exception Description */public String exportexcel (Xmldataobject args,string czrydm) throws Exception {//before receiving
The query parameters passed by the table String RWMC = Stringex.snull (Args.getitemvalue ("RWMC"));
String rwlb = Stringex.snull (Args.getitemvalue ("RWLB")); if (null!= RWMC &&! "".
Equals (RWMC)) {Sqlwhere.append ("and a.rwmc like '%" +rwmc+ "%"); } if (null!= RWLB &&! "".
Equals (RWLB)) {Sqlwhere.append ("and a.rwlb_dm = '" +rwlb+ ");
} StringBuilder sql = new StringBuilder (); Stitching query SQL Sql.append ("Select RWXH,RWMC from Rwxx"). Append (Sqlwhere). Append ("ORDER by Rwxh"). Append (sqliswhe
RE);
Exported column headers string[] titles = {"Task ordinal", "Task Name"};
Instantiate public class Createexcel Excel = new Createexcel ();
Return Excel.newtoexcel (Sql.tostring (), Czrydm,titles, "Exlcel head"); }
The above Java program to achieve the export of Excel method (support IE low version) is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.