Export an excel table

Source: Internet
Author: User

Export an excel table

Prerequisites: import the jar package

 1 <dependency> 2     <groupId>org.apache.poi</groupId> 3     <artifactId>poi-ooxml</artifactId> 4     <version>3.10-FINAL</version> 5 </dependency> 6 <dependency> 7     <groupId>org.apache.poi</groupId> 8     <artifactId>poi</artifactId> 9     <version>3.17</version>10 </dependency>11 <dependency>12     <groupId>junit</groupId>13     <artifactId>junit</artifactId>14     <version>4.11</version>15     <scope>test</scope>16 </dependency>

Set the fields in the database table to the table header, and write the corresponding records to the excel file at a time.

Background code:

Import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import java. io. outputStream; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import org. apache. poi. hssf. usermodel. HSSFCell; import org. apache. poi. hssf. usermodel. HSSFCellStyle; import org. apache. poi. hssf. usermodel. HSSFFont; import org. apache. poi. hssf. usermodel. HSSFPatternFormatti Ng; import org. apache. poi. hssf. usermodel. HSSFRow; import org. apache. poi. hssf. usermodel. HSSFSheet; import org. apache. poi. hssf. usermodel. HSSFWorkbook; import org. apache. poi. hssf. util. cellRangeAddress; import org. apache. poi. hssf. util. HSSFColor; import org. junit. test;/*** export excel * @ author hewangtong **/@ SuppressWarnings ("deprecation ") public class ExportExcel {/*** export excel * @ param out where to output * @ param st Udents student information * @ param title content title in excel * @ param headers header label in the student information table */public void exportExcel (OutputStream out, List <Map <String, Object> students, string title, String [] headers) {// 1. create a workbook. HSSFWorkbook workbook = new HSSFWorkbook (); // 2. create a sheet page HSSFSheet sheet = workbook. createSheet ("student-info"); // 3. set the column width of the sheet page. setdefacolumcolumnwidth (30); // 4. solve the problem: // set the title style HSSFCellStyle titleCellStyle = workbook. createCellStyle (); // create the title style titleCellStyle. setAlignment (HSSFCellStyle. ALIGN_CENTER); // center the title. // set the title Font HSSFFont titleFont = workbook. createFont (); titleFont. setBoldweight (HSSFFont. BOLDWEIGHT_BOLD); titleFont. setFontHeight (short) 8); titleFont. setFontHeightInPoints (short) 16); titleFont. setFontName (""); titleFont. setCharSet (HSSFFont. DEFAULT_CHARSET); // implant the font style into the titleCellStyle. setFont (titleFont); // set the title Position HSSFRow titleRow = sheet. createRow (0); // The first line HSSFCell titleCell = titleRow. createCell (0); // place the title value and style in the corresponding position titleCell. setCellValue (title); titleCell. setCellStyle (titleCe LlStyle); // merge cell CellRangeAddress cra = new CellRangeAddress (0, 0, 0, 3); sheet. addMergedRegion (cra); // 5. solve the Problem of table Header/student statistical table header HSSFRow headerRow = sheet. createRow (1); // HSSFCell headerCell = null in the second row; // set the Header style HSSFCellStyle headerStyle = workbook. createCellStyle (); headerStyle. setAlignment (HSSFCellStyle. ALIGN_CENTER); headerStyle. setBorderBottom (HSSFCellStyle. BORDER_THIN); // set the border headerStyle. setBorder Left (HSSFCellStyle. BORDER_THIN); headerStyle. setBorderRight (HSSFCellStyle. BORDER_THIN); headerStyle. setBorderTop (HSSFCellStyle. BORDER_THIN); // The following two types must be used with headerStyle. setFillForegroundColor (HSSFColor. ORANGE. index); // fill in the foreground color headerStyle. setFillPattern (HSSFPatternFormatting. SOLID_FOREGROUND); // fill mode // set the header font HSSFFont headerFont = workbook. createFont (); headerFont. setBoldweight (HSSFFont. BOLDWEIGHT_NORM AL); headerFont. setCharSet (HSSFFont. DEFAULT_CHARSET); headerFont. setColor (HSSFColor. GREEN. index); headerFont. setFontName (""); headerFont. setFontHeight (short) 6); headerFont. setFontHeightInPoints (short) 12); headerStyle. setFont (headerFont); for (int I = 0; I 

The effect is as follows:

Reference: http://blog.csdn.net/he140622hwt/article/details/78467457

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.