微軟的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