Import and Export of simple excel

Source: Internet
Author: User

Assembly to be referenced



List <type> lists = new List <type>; // The data is stored in the worksheet. However, you must create a worksheet first. With the workbook, you can create a worksheet according to the workbook // 1. create a work thin HSSFWorkbook workbook = new HSSFWorkbook (); // 2. create a worksheet/HSSFSheet sheet1 = new HSSFSheet (workbook); hssfsheet1 = workbook. createSheet (); // 3. enter the data for (int I = 0; I <lists. count; I ++) {// Each object corresponds to a row of data // 4. create the data row object HSSFRow row = sheet1.CreateRow (I) for the sheet table first; // 5.1 Add data for each column of the row --- create a travel column first
HSSFCell cell1 = row. CreateCell (0); // 5.2 assigns cell1.SetCellValue (lists [I]. CID) to this column );
HSSFCell cell2 = row. createCell (1); // 5.2 assigns cell2.SetCellValue (lists [I] to this column. CName); // after the workbook is created, you also need to use the file stream to write data to the physical file using (FileStream fs = new FileStream (@ "C: \ Users \ john \ Desktop \ 11.xls", FileMode. create) {workbook. write (fs );}


// Import: Read the specified file using (FileStream fs = new FileStream (@ "C: \ Users \ john \ Desktop \ 11.xls", FileMode. open) {// 1. generate a work thin HSSFWorkbook workbook = new HSSFWorkbook (fs); // 2. you need to retrieve the worksheet HSSFSheet sheet = workbook in this workbook. getSheetAt (0); // 3. read each row of data in this table cyclically. because writing from that row can be modified, FirstRowNum records the row to be written, and LastRowNum records the last row StringBuilder sb = new StringBuilder (); list <type> lists = new List <type> (); for (int row = sheet. firstRowNum; row <= sheet. lastRowNum; row ++) {// 4 each row of data corresponds to an object MODEL. classes clas = new MODEL. classes (); // first extract this line HSSFRow curow = sheet. getRow (row); clas. CID = (int) curow. getCell (0 ). numericCellValue; clas. CName = curow. getCell (1 ). stringCellValue; clas. CCount = (int) curow. getCell (2 ). numericCellValue; clas. CImg = curow. getCell (3 ). stringCellValue; clas. CIsDel = (bool) curow. getCell (4 ). booleanCellValue; clas. CAddTime = Convert. toDateTime (curow. getCell (5 ). dateCellValue); lists. add (clas );}


Import and Export of simple excel

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: 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.