c# .Net :Excel NPOI導入導出操作教程之List集合的數據寫到一個Excel文件並導出


將List集合的數據寫到一個Excel文件並導出示例:


using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;

 

  List<UserInfo> listUser = new List<UserInfo>()
           {
               new UserInfo { name="1", id="1", phone="1r" },
               new UserInfo { name="2", id="2", phone="2r" },
               new UserInfo { name="3", id="3", phone="3r" },
               new UserInfo { name="4", id="4", phone="4r" },
               new UserInfo { name="5", id="5", phone="5r" },
           };
        1、//創建工作簿對象
       IWorkbook workbook = new HSSFWorkbook();
        2、//創建工作表
        ISheet sheet = workbook.CreateSheet("onesheet");
        IRow row0 = sheet.CreateRow(0);
        row0.CreateCell(0).SetCellValue("用戶Id");
        row0.CreateCell(1).SetCellValue("用戶名稱");
        row0.CreateCell(2).SetCellValue("用戶備注信息");
        for (int r = 1; r < listUser.Count; r++)
        {
            3、//創建行row
            IRow row = sheet.CreateRow(r);
            row.CreateCell(0).SetCellValue(listUser[r].id);
            row.CreateCell(1).SetCellValue(listUser[r].name);
            row.CreateCell(2).SetCellValue(listUser[r].phone);
            row.CreateCell(3).SetCellValue(listUser[r].pwd);
        }

    //創建流對象並設置存儲Excel文件的路徑
        using (FileStream url = File.OpenWrite(@"C:\Users\Administrator\Desktop\寫入excel.xls"))
        {

    //導出Excel文件
            workbook.Write(url);
            Response.Write("<script>alert('寫入成功!')</script>");
        };

——————————分享End——————————


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM