Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

OAF exports query results to EXCEL format files

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

PG: create a button of type button, and set the button's Action Type to firePartialAction and Event to exportexcel. (the button type does not use submitButton because the button is of submitButton type. After export, adding rows to the page or saving will prompt the browser to report a fallback exception).

CO:processFormRequest Code:

If ("exportexcel" .equals (pageContext.getParameter (EVENT_PARAM) {

Byte abtye0 [] = (byte []) am.invokeMethod ("export")

Try {

/ / obtain DataObject

DataObject dataObject = pageContext.getNamedDataObject ("_ SessionParameters")

/ / obtain response according to DataObject

HttpServletResponse httpservletresponse = (HttpServletResponse) dataObject.selectValue (null, "HttpServletResponse")

Download (pageContext, httpservletresponse, abtye0)

} catch (Exception e) {

E.printStackTrace ()

Throw OAException.wrapperException (e)

}

}

Download method code:

Public void download (OAPageContext pageContext

HttpServletResponse response, byte [] abyte0) {

String fileName = "export"

/ / fileName = pageContext.getMessage ("CUX", "CUX_SRM_IMPORT_FILE_NAME", null)

Try {

String charset =

PageContext.getProfile ("ICX_CLIENT_IANA_ENCODING")

/ / sets the format character set of the file

Response.setContentType ("application/vnd.ms-excel;charset=" +

Charset) / / gb2312

/ / set the file size

Response.setContentLength (abyte0.length)

/ / throw new OAException ("abyte0.length:" + abyte0.length,OAException.ERROR)

/ / notify browser of the name of the file

Response.setHeader ("Content-Disposition"

"attachment;filename=" + fileName + ".xls")

/ / get the output stream

OutputStream toClient = response.getOutputStream ()

/ / write the character array to the output stream

ToClient.write (abyte0)

/ / forced refresh (download box pops up on the page)

ToClient.flush ()

/ / close the output stream

ToClient.close ()

} catch (Exception ex) {

Ex.printStackTrace ()

}

} / / end download ()

AM,export Code:

Public byte [] export () {

Byte abyte0 [] = null

CuxAslVOImpl vo = getCuxAslVO1 ()

CuxAslVORowImpl hRow = null

Int rowcount = vo.getRowCount (); / / fetch the number of records of the currently extracted recordset

If (rowcount = = 0)

Throw new OAException ("No data to be exported", OAException.ERROR)

RowSetIterator deleteIter =

Vo.createRowSetIterator ("deleteIter"); / / create an indicator for the recordset

DeleteIter.setRangeStart (0); / / sets the start point of the loop, which is equivalent to moving the pointer to the first record

DeleteIter.setRangeSize (rowcount); / / set the number of cycles

/ / the first step is to create a webbook that corresponds to an Excel file

HSSFWorkbook wb = new HSSFWorkbook ()

/ / the second step is to add a sheet to the webbook that corresponds to the sheet in the Excel file

HSSFSheet sheet = wb.createSheet ("export")

/ / the third step is to add row 0 of the header to sheet. Note that there is a limit on the number of rows and columns of Excel in the old version of poi. Short

HSSFRow row = sheet.createRow ((int) 0)

/ / step 4, create the cell and set the value header to center the header

CellStyle style = wb.createCellStyle ()

Style.setAlignment (CellStyle.ALIGN_CENTER); / / create an in-play format

/ / CSV

StringBuffer buffer =

New StringBuffer ("inventory organization name, material code, material description, supplier code, supplier name, supplier location, status, whether disabled, settlement method\ r\ n")

/ / Excel

HSSFCell cell = row.createCell ((short) 0)

Cell.setCellValue ("inventory Organization name")

Cell.setCellStyle (style)

Cell = row.createCell ((short) 1)

Cell.setCellValue ("material Code")

Cell.setCellStyle (style)

Cell = row.createCell ((short) 2)

Cell.setCellValue ("material description")

Cell.setCellStyle (style)

Cell = row.createCell ((short) 3)

Cell.setCellValue (Vendor Code)

Cell.setCellStyle (style)

Cell = row.createCell ((short) 4)

Cell.setCellValue ("supplier name")

Cell.setCellStyle (style)

Cell = row.createCell ((short) 5)

Cell.setCellValue ("supplier location")

Cell.setCellStyle (style)

Cell = row.createCell ((short) 6)

Cell.setCellValue ("status")

Cell.setCellStyle (style)

Cell = row.createCell ((short) 7)

Cell.setCellValue ("disable or not")

Cell.setCellStyle (style)

Cell = row.createCell ((short) 8)

Cell.setCellValue ("settlement method")

Cell.setCellStyle (style)

For (int I = 0; I < rowcount; iTunes +) {

Row = sheet.createRow ((int) I + 1)

HRow = (CuxAslVORowImpl) deleteIter.getRowAtRangeIndex (I); / / get the current record

/ / step 5, create the cell and set the value

Row.createCell ((short) 0) .setCellValue (hRow.getOrganizationName ())

Row.createCell ((short) 1) .setCellValue (hRow.getItemNumber ())

Row.createCell ((short) 2) .setCellValue (hRow.getItemDesc ())

Row.createCell ((short) 3) .setCellValue (hRow.getVendorNumber ())

Row.createCell ((short) 4) .setCellValue (hRow.getVendorName ())

Row.createCell ((short) 5) .setCellValue (hRow.getVendorSiteCode ())

Row.createCell ((short) 6) .setCellValue (hRow.getAslStatus ())

Row.createCell ((short) 7) .setCellValue (hRow.getDisableFlag ())

Row.createCell ((short) 8) .setCellValue (hRow.getPoSettlementMethod ())

Buffer.append (hRow.getOrganizationName () + "," +

HRow.getItemNumber () + "," + hRow.getItemDesc () +

"," + hRow.getVendorNumber () + "," +

HRow.getVendorName () + "," +

HRow.getVendorSiteCode () + "," +

HRow.getAslStatus () + "," + hRow.getDisableFlag () +

"," + hRow.getPoSettlementMethod () + "\ r\ n")

}

/ / step 6, convert the file to an byte array

Try {

/ / there are two export formats, CSV is masked and EXCEL is unmasked

/ / Excel

/ / the file can only be converted into a stream and into a binary array through the byte stream (the stream cannot be serialized to make the parameter output)

ByteArrayOutputStream os = new ByteArrayOutputStream ()

/ / File write stream

Wb.write (os)

/ / transfer array

Abyte0 = os.toByteArray ()

/ / close the stream

Os.close ()

/ CSV

/ characters are transferred directly to byte array

/ / abyte0 = buffer.toString () .getBytes ()

} catch (Exception e) {

E.printStackTrace ()

}

DeleteIter.closeRowSetIterator ()

Return abyte0

} / / end export ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report