Export Excel Data Based on POI and POI export Excel Data
Export Excel Data Based on POI
There are many report services in the project and data needs to be exported. Here we use the POI-based method for parsing. POI has two Resolution Methods: HSSF (xls format) and XSSF (xlsx ).
Take the BOS logistics waybill management interface as an example:
Front-end code
Export the waybill information and generate a report:
1. Add export button
2. Add export events
Background code implementation
The process of writing data to an Excel file in the Excel file generated by POI is the same as that of creating an Excel file-creating a Sheet-creating a Row-creating a Cell-writing Cell data.
For details about how to import the jar package to Maven, see the configuration in one-click upload.
Action-Layer Code Implementation
@ Controller @ Scope ("prototype") @ ParentPackage ("json-default") @ Namespace ("/") public class ExportXlsAction extends BaseAction <WayBill >{@ Autowired private WayBillService wayBillService; @ Action ("report_exportXls") public String exportXls () throws Exception {// query the List of result data that meets the current condition <WayBill> waybills = wayBillService. findwayBills (model); // generate an Excel file (in xls format) HSSFWorkbook hssfWorkbook = new HSSFWorkbook (); HSSFSheet sheet = hssfWorkbook. createSheet ("waybill data"); // create a sheet object // header (the first row of the table) HSSFRow headRow = sheet. createRow (0); // create the first row object headRow. createCell (0 ). setCellValue ("waybill number"); // you have created the first grid. Its attribute is the waybill number headRow. createCell (1 ). setCellValue ("sender"); // creates the second lattice... headRow. createCell (2 ). setCellValue ("sender phone"); headRow. createCell (3 ). setCellValue ("sender address"); headRow. createCell (4 ). setCellValue ("recipient"); headRow. createCell (5 ). setCellValue ("recipient phone number"); headRow. createCell (6 ). setCellValue ("recipient address"); // table data (the Retrieved Data corresponds to a row of records for an object) for (WayBill wayBill: waybills) {// get the row number HSSFRow dataRow = sheet in the last row. createRow (sheet. getLastRowNum () + 1); dataRow. createCell (0 ). setCellValue (wayBill. getWayBillNum (); dataRow. createCell (1 ). setCellValue (wayBill. getSendName (); dataRow. createCell (2 ). setCellValue (wayBill. getSendMobile (); dataRow. createCell (3 ). setCellValue (wayBill. getSendAddress (); dataRow. createCell (4 ). setCellValue (wayBill. getRecName (); dataRow. createCell (5 ). setCellValue (wayBill. getRecMobile (); dataRow. createCell (6 ). setCellValue (wayBill. getRecAddress ();} // download and export // set the header information (set the object type xls) ServletActionContext. getResponse (). setContentType ("application/vnd. ms-excel "); String filename =" waybill data .xls "; String agent = ServletActionContext. getRequest (). getHeader ("user-agent"); // obtain the browser type (Firefox, 360, IE) filename = FileUtils. encodeDownloadFilename (filename, agent); ServletActionContext. getResponse (). setHeader ("Content-Disposition", "attachment; filename =" + filename); // filename: sets the file name FileUtils for the production Excel file: it is a Chinese encoding class ServletOutputStream outputStream = ServletActionContext for the browser type. getResponse (). getOutputStream (); // get a response Stream object hssfWorkbook. write (outputStream); // close hssfWorkbook. close (); return NONE ;}}Action-Layer Code
Package cn. itcast. bos. utils; import java. io. IOException; import java.net. URLEncoder; import sun. misc. BASE64Encoder; public class FileUtils, encode the attachment name ** @ param filename * download file name * @ param agent * client browser * @ return encoded Download Attachment name * @ throws IOException */public static String encodeDownloadFilename (string filename, string agent) throws IOException {if (agent. contains ("Firefox") {// Firefox Filename = "=? UTF-8? B? "+ New BASE64Encoder (). encode (filename. getBytes (" UTF-8 ") + "? = "; Filename = filename. replaceAll ("\ r \ n", "");} else {// IE and other browsers filename = URLEncoder. encode (filename, "UTF-8"); filename = filename. replace ("+", "");} return filename ;}}FileUtils class
Public List <WayBill> findwayBills (WayBill wayBill) {if (StringUtils. isBlank (wayBill. getWayBillNum () & StringUtils. isBlank (wayBill. getSendAddress () & StringUtils. isBlank (wayBill. getRecAddress () & StringUtils. isBlank (wayBill. getSendProNum () & (wayBill. getSignStatus () = null | wayBill. getSignStatus () = 0) {// unconditionally queries and queries the database return wayBillRepository. findAll ();} else {// the query condition // The must condition must be The and // must not condition must not be set. not // The shocould condition can be set up or BoolQueryBuilder query = new BoolQueryBuilder (); // Boolean query, multi-condition Combined Query // Add the condition if (StringUtils. isNoneBlank (wayBill. getWayBillNum () {// wayBill number query QueryBuilder tempQuery = new TermQueryBuilder ("wayBillNum", wayBill. getWayBillNum (); query. must (tempQuery);} if (StringUtils. isNoneBlank (wayBill. getSendAddress () {// fuzzy query of the place of shipment // Case 1: Enter "North" as part of the query entry, and use fuzzy QueryBuilder wildcardQuery = new WildcardQueryBuilder ("sendAddress", "*" + wayBill. getSendAddress () + "*"); // Scenario 2: The input "Haidian District, Beijing" is a combination of multiple entries. After word segmentation, each entry matches the query QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder (wayBill. getSendAddress ()). field ("sendAdress "). defaultOperator (Operator. AND); // The relationship BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder (); boolQueryBuilder. shocould (query StringQueryBuilder); boolQueryBuilder. shocould (wildcardQuery); query. must (boolQueryBuilder);} if (StringUtils. isNoneBlank (wayBill. getRecAddress () {// fuzzy query of the receiving location QueryBuilder wildcardQuery = new WildcardQueryBuilder ("recAddress", "*" + wayBill. getRecAddress () + "*"); query. must (wildcardQuery);} if (StringUtils. isNoneBlank (wayBill. getSendProNum () {// saved data type equivalent query QueryBuilder termQuery = new TermQ UeryBuilder ("sendProNum", wayBill. getSendProNum (); query. must (termQuery);} if (StringUtils. isNoneBlank (wayBill. getSendProNum () {// saved type equivalent query QueryBuilder termQuery = new TermQueryBuilder ("sendProNum", wayBill. getSendProNum (); query. must (termQuery);} if (wayBill. getSignStatus ()! = Null & wayBill. getSignStatus ()! = 0) {// query the receipt status QueryBuilder termQuery = new TermQueryBuilder ("signStatus", wayBill. getSignStatus (); query. must (termQuery);} SearchQuery searchQuery = new NativeSearchQuery (query); // Integer. MAX_VALUE: it is convenient to obtain all the queried data Pageable pageable = new PageRequest (0, Integer. MAX_VALUE); searchQuery. setPageable (pageable); // page effect // conditional query and query index database return wayBillIndexRepository. search (searchQuery ). getContent ();}}Service Layer Code
Package cn. itcast. bos. dao. index; import org. springframework. data. elasticsearch. repository. elasticsearchRepository; import cn. itcast. bos. domain. take_delivery.WayBill; // The multi-condition query uses the cache public interface WayBillIndexRepository extends ElasticsearchRepository <WayBill, Integer> {}Dao-Layer Code Implementation
Final effect:
Because I do not know much about POI APIs, the printed Excel Data Layout is adjusted manually.