C# WinForm導(dǎo)出Excel方法介紹
.NET開發(fā)人員首選的方法,通過COM組件調(diào)用Office軟件本身來實(shí)現(xiàn)文件的創(chuàng)建和讀寫,但是數(shù)據(jù)量較大的時(shí)候異常緩慢;如下代碼所示已經(jīng)做了優(yōu)化,將一個(gè)二維對(duì)象數(shù)組賦值到一個(gè)單元格區(qū)域中(下面的代碼中只能用于導(dǎo)出列數(shù)不多于26列的數(shù)據(jù)導(dǎo)出):
Office PIA
public static void ExportToExcel(DataSet dataSet, string outputPath)
{
Excel.ApplicationClass excel = new Excel.ApplicationClass();
Excel.Workbook workbook = excel.Workbooks.Add(Type.Missing);
int sheetIndex = 0;
foreach (System.Data.DataTable dt in dataSet.Tables)
{
object[,] data = new object[dt.Rows.Count + 1, dt.Columns.Count];
for (int j = 0; j < dt.Columns.Count; j++)
{
data[0, j] = dt.Columns[j].ColumnName;
}
for (int j = 0; j < dt.Columns.Count; j++)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
data[i + 1, j] = dt.Rows[i][j];
}
}
string finalColLetter = string.Empty;
string colCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int colCharsetLen = colCharset.Length;
if (dt.Columns.Count > colCharsetLen)
{
finalColLetter = colCharset.Substring(
(dt.Columns.Count - 1) / colCharsetLen - 1, 1);
}
finalColLetter += colCharset.Substring(
(dt.Columns.Count - 1) % colCharsetLen, 1);
Excel.Worksheet sheet = (Excel.Worksheet)workbook.Sheets.Add(
workbook.Sheets.get_Item(++sheetIndex),
Type.Missing, 1, Excel.XlSheetType.xlWorksheet);
sheet.Name = dt.TableName;
string range = string.Format("A1:{0}{1}", finalColLetter, dt.Rows.Count + 1);
sheet.get_Range(range, Type.Missing).Value2 = data;
((Excel.Range)sheet.Rows[1, Type.Missing]).Font.Bold = true;
}
workbook.SaveAs(outputPath, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
workbook.Close(true, Type.Missing, Type.Missing);
workbook = null;
excel.Quit();
KillSpecialExcel(excel);
excel = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
static void KillSpecialExcel(Excel.Application app)
{
try
{
if (app != null)
{
int processId;
GetWindowThreadProcessId(new IntPtr(app.Hwnd), out processId);
System.Diagnostics.Process.GetProcessById(processId).Kill();
}
}
catch (Exception ex)
{
throw ex;
}
}
文件流
這種方法的效率明顯高于第一種,而且也不需要安裝Office,但是導(dǎo)出的xls文件并不符合Excel的格式標(biāo)準(zhǔn),在打開生成的xls文件時(shí)會(huì)提示:The file you are trying to open is in a different format that specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file.
public static void ExportToExcel(System.Data.DataSet ds, string path)
{
StreamWriter sw = null;
try
{
long totalCount = ds.Tables[0].Rows.Count;
sw = new StreamWriter(path, false, Encoding.Unicode);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
sb.Append(ds.Tables[0].Columns[i].ColumnName + "\t");
}
sb.Append(Environment.NewLine);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
sb.Append(ds.Tables[0].Rows[i][j].ToString() + "\t");
}
sb.Append(Environment.NewLine);
}
sw.Write(sb.ToString());
sw.Flush();
}
catch (IOException ioe)
{
throw ioe;
}
finally
{
if (sw != null)
{
sw.Close();
}
}
}
相關(guān)文章
C#控件Picturebox實(shí)現(xiàn)鼠標(biāo)拖拽功能
這篇文章主要為大家詳細(xì)介紹了C#控件Picturebox實(shí)現(xiàn)鼠標(biāo)拖拽功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09那些年,我還在學(xué)習(xí)C# 學(xué)習(xí)筆記續(xù)
那些年學(xué)習(xí)C#,就是對(duì)C#相關(guān)的一些知識(shí)有一個(gè)了解,等到要用時(shí)才不會(huì)找不到方向,比如說擴(kuò)展方法,開始時(shí)怎么覺得沒有用,后來了解到asp.net MVC,它可以用來擴(kuò)展Html類,比如做一個(gè)分頁的方法;所以對(duì)一門語言了解寬一些是沒有壞處的2012-03-03C#設(shè)計(jì)模式之觀察者模式實(shí)例講解
這篇文章主要介紹了C#設(shè)計(jì)模式之觀察者模式實(shí)例講解,本文詳細(xì)講解了觀察者模式的定義、優(yōu)缺點(diǎn)、代碼實(shí)例等,需要的朋友可以參考下2014-10-10C# winform實(shí)現(xiàn)登陸次數(shù)限制
這篇文章主要介紹了C# winform實(shí)現(xiàn)登陸次數(shù)限制,相信大家都遇到過網(wǎng)站在用戶多次輸錯(cuò)密碼之后會(huì)自動(dòng)把賬戶凍結(jié)的情況,這種功能如何實(shí)現(xiàn),下面小編為大家分享實(shí)現(xiàn)方法2016-05-05關(guān)于C#中yield關(guān)鍵字的深入解析
這篇文章主要給大家介紹了關(guān)于C#中yield關(guān)鍵字的深入解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11C#導(dǎo)出GridView數(shù)據(jù)到Excel文件類實(shí)例
這篇文章主要介紹了C#導(dǎo)出GridView數(shù)據(jù)到Excel文件類,實(shí)例分析了C#使用GridView及Excel的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03支持多類型數(shù)據(jù)庫的c#數(shù)據(jù)庫模型示例
本文為大家提供一個(gè)c#數(shù)據(jù)庫訪問模型,支持多類型數(shù)據(jù)庫,簡(jiǎn)單抽取數(shù)據(jù)庫訪問函數(shù),大家參考使用吧2014-01-01WPF實(shí)現(xiàn)圖片合成或加水印的方法【2種方法】
這篇文章主要介紹了WPF實(shí)現(xiàn)圖片合成或加水印的方法,結(jié)合實(shí)例形式分析了2種比較實(shí)用的WPF圖片操作相關(guān)技巧,需要的朋友可以參考下2017-03-03