Java-based excel template export and javaexcel template Export

Source: Internet
Author: User

Java-based excel template export and javaexcel template Export

I. Preparations

1. Click here to download related development tools

2. Put the poi-3.8 and jxls-core-1.0 jar packages into the project and reference

3. Place the exceltemplate runrecord.xls to the RunRecordBSImpl. java class path.

 

Ii. RunRecordBSImpl. java class

1 import java. io. bufferedInputStream; 2 import java. io. fileInputStream; 3 import java. io. fileOutputStream; 4 import java. io. inputStream; 5 import java. io. outputStream; 6 import java. util. arrayList; 7 import java. util. hashMap; 8 import java. util. list; 9 import java. util. map; 10 11 import org. apache. poi. hssf. usermodel. HSSFSheet; 12 import org. apache. poi. ss. usermodel. workbook; 13 import org. apache. poi. ss. Util. cellRangeAddress; 14 15 import net. sf. jxls. transformer. XLSTransformer; 16 17 public class RunRecordBSImpl {18 public static void main (String [] args) {19 RunRecordBSImpl test = new RunRecordBSImpl (); 20 test. export (); 21} 22 23 public void export () {24 String templateFileName = "runRecord.xls"; 25 String templateFilePath = this. getClass (). getResource (""). getPath (); 26 String destFileName = "device operation record card. x Ls "; 27 28 Map <String, Object> beans = new HashMap <String, Object> (); 29 30 // device information 31 Map <String, object> product = new HashMap <String, Object> (); 32 product. put ("model", "XG2016"); 33 product. put ("version", "V1.0"); 34 beans. put ("product", product); 35 36 // year, month, and day 37 beans. put ("year", "2016"); 38 beans. put ("month", "08"); 39 beans. put ("day", "08"); 40 41 // running record 42 Map <String, Object> record = new HashMap <String, Object> (); 43 record. put ("inputVoltage", "200"); 44 record. put ("inputVoltageStatus", 0); 45 record. put ("inputCurrent", "50"); 46 record. put ("inputCurrentStatus", 0); 47 beans. put ("record", record); 48 49 // Exception Handling 50 List <Map <String, Object> exceptions = new ArrayList <Map <String, Object> (); 51 Map <String, Object> e1 = new HashMap <String, Object> (); 52 e1.put ("detail", "input voltage too large"); 53 e1.put ("handle ", "Lower input voltage"); 54 e1.put ("handleMode", 1); 55 exceptions. add (e1); 56 57 Map <String, Object> e2 = new HashMap <String, Object> (); 58 e2.put ("detail", "the input current is too large "); 59 e2.put ("handle", "lower input current"); 60 e2.put ("handleMode", 1); 61 exceptions. add (e2); 62 63 beans. put ("exceptions", exceptions); 64 65 InputStream in = null; 66 OutputStream out = null; 67 68 try {69 XLSTransformer transformer = new XLSTransformer (); 70 in = ne W BufferedInputStream (new FileInputStream (templateFilePath + templateFileName); 71 Workbook workbook = transformer. transformXLS (in, beans); 72 HSSFSheet sheet = (HSSFSheet) workbook. getSheetAt (0); 73 74 int startMergeLine = 7; // number of rows that start merging 75 // list of merged Exception Handling cells 76 if (exceptions. size ()> 0) {77 sheet. addMergedRegion (new CellRangeAddress (startMergeLine, startMergeLine + exceptions. size ()-1, 0, 0); 78} 79 80 // write and enter the content Outbound and All cached content is sent out 81 out = new FileOutputStream (templateFilePath + destFileName); 82 workbook. write (out); 83 out. flush (); 84} catch (Exception e) {85 e. printStackTrace (); 86} finally {87 try {88 if (out! = Null) 89 out. close (); 90 if (in! = Null) 91 in. close (); 92} catch (Exception e) {93 e. printStackTrace (); 94} 95} 96} 97}View Code


Iii. Run the test

Run the RunRecordBSImpl. java class as a java application. Under this class path, you can generate the "device operation record. xls" file according to the excel template.

  

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.