Assembly to be referenced
Export:
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:
// 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