C#高效匯出Excel(IList轉DataTable,DataSet)

來源:互聯網
上載者:User

微軟的Excel操作類匯出Excel會很慢,此方法簡單的把表中內容以字串的形式寫入到Excel中,用到的一個技巧就是"\t".

C#中的\t相當於Tab鍵,寫入到Excel中時就是一列一列中寫入。

引用命名空間:

using System.Drawing;using System.Threading;using System.IO;using System.Data;using System.Text;using System.Collections;

 

protected void btnExport_Click(object sender, EventArgs e)    {        this.labPercent.Text = "";        IList<ViewUserInfo> userList = viewUserInfoService.GetUserInfoListAll();        DataTable dt = IListOut(userList);        WriteExcel(dt, "d:\\a.xls");    }    #region 匯出Excel    public void WriteExcel(DataTable ds, string path)    {        long totalCount = ds.Rows.Count;        Thread.Sleep(1000);        long rowRead = 0;        float percent = 0;        StreamWriter sw = new StreamWriter(path, false, Encoding.GetEncoding("gb2312"));        StringBuilder sb = new StringBuilder();        for (int k = 0; k < ds.Columns.Count; k++)        {            sb.Append(ds.Columns[k].ColumnName.ToString() + "\t");        }        sb.Append(Environment.NewLine);        for (int i = 0; i < ds.Rows.Count; i++)        {            //rowRead++;            //percent = ((float)(100 * rowRead)) / totalCount;            this.labPercent.Text ="<a href='UserInfo.xls' target='_blank'>此處下載</a>";            for (int j = 0; j < ds.Columns.Count; j++)            {                sb.Append(ds.Rows[i][j].ToString() + "\t");            }            sb.Append(Environment.NewLine);        }        sw.Write(sb.ToString());        sw.Flush();        sw.Close();    }    public DataTable IListOut(IList<ViewUserInfo> ResList)    {        DataTable TempDT = new DataTable();        //此處遍曆IList的結構並建立同樣的DataTable        System.Reflection.PropertyInfo[] p = ResList[0].GetType().GetProperties();        foreach (System.Reflection.PropertyInfo pi in p)        {            TempDT.Columns.Add(pi.Name, System.Type.GetType(pi.PropertyType.ToString()));        }        for (int i = 0; i < ResList.Count; i++)        {            ArrayList TempList = new ArrayList();            //將IList中的一條記錄寫入ArrayList            foreach (System.Reflection.PropertyInfo pi in p)            {                object oo = pi.GetValue(ResList[i], null);                TempList.Add(oo);            }            object[] itm = new object[p.Length];            //遍曆ArrayList向object[]裡放資料            for (int j = 0; j < TempList.Count; j++)            {                itm.SetValue(TempList[j], j);            }            //將object[]的內容放入DataTable            TempDT.LoadDataRow(itm, true);        }        //返回DataTable        return TempDT;    }        #endregion

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.