Java POI exporta Excel y lo descarga al cliente. El contenido específico es el siguiente
Se publica la configuración de Maven, incluidas las dependencias para otros formatos de archivo
<Spendency> <MoupRoD> org.apache.poi </groupid> <artifactid> poi-excelant </arfactid> <verserse> 3.12 </versión> </pendency> <ependency> <grupoD> org.apache.poi </groupid> <arifactidid> poi-scratchpad </artifactid> <ripsel <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.8</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.8</version> </dependency>
Capa de servicio
@Override public void export (long sblsh, string ExcelName, outputStream out) {try {// El primer paso es crear un libro web, correspondiente a un archivo de Excel hssfworkbook wb = new hssfworkbook (); // Generar una tabla HSSFSheet Sheet = WB.CreateSheet (ExcelName); // El tercer paso es agregar la fila 0 del encabezado de la tabla en la hoja hssfrow fila = sheet.createrow (0); // El cuarto paso es crear una celda y establecer el encabezado de la tabla de valor para configurar el encabezado de la tabla en el centro HSSFCellStyle Style = WB.CreateCellStyle (); style.setalignment (hssfcellstyle.align_center); // Crear un formato centrado HSSFCell Cell = Row.CreateCell (0); Cell.SetCellStyle (estilo); Byte kjzz = qyjbxxmapper.getkjzz (sblsh); Lista <a> registro = this.selectbysblsh (sblsh); this.insertData (wb, hoja, fila, registro, fuera); }} catch (excepción e) {logger.info (e.getMessage ()); }} / *** Importar datos en una tabla* @param wb execl archivo* @param tabla de hojas* @param fila tabla fila* @param Los datos de registro que se exportarán* @param out fl. for (int i = 0; i <title.length; i ++) {row.createCell (i) .setCellValue (title [i]); } for (int i = 0; i <registro.size (); i ++) {row = sheet.creatrow (i+2); A data = registro.get (i); Row.CreateCell (0) .SetCellValue (data.gethc ()); Row.CreateCell (1) .SetCellValue (data.getxm ()); BigDecimal je = data.getje (); if (je! = null) {row.createCell (2) .setCellValue (Je.DoubleValue ()); }} // Celillas de fusión, los primeros 2 dígitos representan la fila inicial y final, y los últimos 2 dígitos representan la columna de la columna inicial y final de la región de CellRanGeadDress = new CellRanGeaddress (0,0,0, title.length-1); hoja. wb.write (fuera); out.flush (); out.close (); wb.close (); } catch (excepción e) {logger.info (e.getMessage ()); }} Controlador
@RequestMapping ("/export") Public void export (Long SBLSH, HttpServletRequest Solicitud, respuesta httpServletResponse) {Response.SetContentType ("Octets/Stream"); String ExcelName = "FileName"; Pruebe {Response.AddHeader ("Content-Disposition", "Adjunto; FileName ="+New String (ExcelName.getBytes ("GB2312"), "ISO8859-1")+". XLS"); OutputStream out = Response.getOutputStream (); aservice.export (SBLSH, ExcelName, Out); } catch (Exception e) {E.PrintStackTrace (); }}Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.