Open-source jxls-java Excel (multiple sheet)-template file defines el expressions similar to jsp

Source: Internet
Author: User

I don't want to define an excel template like writing jsp, and then input variables into the excel template like using jstl and el expressions. Now it's easy to use jxls, the core code is very simple. [Java] // call the engine to generate an excel report XLSTransformer transformer = new XLSTransformer (); Workbook workbook = transformer. transformMultipleSheetsList (is, objects, listSheetNames, "list", new HashMap (), 0); www.2cto. comworkbook. write (new FileOutputStream ("c :\\ xlsExportUtils.xls"); let's take a look at how the excel template is defined. Is it like jstl in jsp. The complete code for this test example is as follows: [java] import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import net. sf. jxls. exception. parsePropertyException; import net. sf. jxls. transformer. XLSTransformer; import org. apache. poi. openxml4j. exceptions. invalidFormatException; import org. apache. poi. ss. usermodel. workbook;/*** @ author luju **/public class XlsExportUtil {public static void createExcel () throws ParsePropertyException, InvalidFormatException, IOException {}@ SuppressWarnings ("rawtypes ") public static void main (String [] args) throws ParsePropertyException, InvalidFormatException, IOException {// obtain the Excel template file String fileDir = XlsExportUtil. class. getResource (""). getFile (); String filePath = fileDir + "XlsExportUtilTemplate.xls"; System. out. println ("excel template file:" + filePath); FileInputStream is = new FileInputStream (filePath); // create the Test Data Map <String, Object> map = new HashMap <String, object> (); List <Map <String, Object> list = new ArrayList <Map <String, Object> (); Map <String, object> map1 = new HashMap <String, Object> (); map1.put ("name", "TV"); map1.put ("price", "3000 "); map1.put ("desc", "3D TV"); map1.put ("Remarks", "Chinese test"); Map <String, Object> map2 = new HashMap <String, object> (); map2.put ("name", "Air conditioner"); map2.put ("price", "2000"); map2.put ("desc", "Inverter Air Conditioner "); map1.put ("Remarks", "test Chinese"); list. add (map1); list. add (map2); map. put ("list", list); ArrayList <List> objects = new ArrayList <List> (); objects. add (list); objects. add (list); objects. add (list); objects. add (list); // sheet Name List <String> listSheetNames = new ArrayList <String> (); listSheetNames. add ("1"); listSheetNames. add ("2"); listSheetNames. add ("3"); listSheetNames. add ("4"); // call the engine to generate an excel report XLSTransformer transformer = new XLSTransformer (); Workbook workbook = transformer. transformMultipleSheetsList (is, objects, listSheetNames, "list", new HashMap (), 0); workbook. write (new FileOutputStream ("c :\\ xlsExportUtils.xls "));}}

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.