C#中將ListView中數(shù)據(jù)導(dǎo)出到Excel的實(shí)例方法
更新時(shí)間:2013年04月26日 15:37:48 作者:
首先 你需要添加引用Microsoft Excel 11.0 Object Library
添加方法:選擇項(xiàng)目->引用->右擊“添加引用”->選擇COM 找到上面組件—>點(diǎn)擊“確定”。
實(shí)現(xiàn)代碼如下:
復(fù)制代碼 代碼如下:
private void 導(dǎo)出數(shù)據(jù)_Click(object sender, EventArgs e)
{
ExportToExecl();
}
/// <summary>
/// 執(zhí)行導(dǎo)出數(shù)據(jù)
/// </summary>
public void ExportToExecl()
{
System.Windows.Forms.SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "xls";
sfd.Filter = "Excel文件(*.xls)|*.xls";
if (sfd.ShowDialog() == DialogResult.OK)
{
DoExport(this.lstPostion, sfd.FileName);
}
}
/// <summary>
/// 具體導(dǎo)出的方法
/// </summary>
/// <param name="listView">ListView</param>
/// <param name="strFileName">導(dǎo)出到的文件名</param>
private void DoExport(ListView listView, string strFileName)
{
int rowNum = listView.Items.Count;
int columnNum = listView.Items[0].SubItems.Count;
int rowIndex = 1;
int columnIndex = 0;
if (rowNum == 0 || string.IsNullOrEmpty(strFileName))
{
return;
}
if (rowNum > 0)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
if (xlApp == null)
{
MessageBox.Show("無法創(chuàng)建excel對象,可能您的系統(tǒng)沒有安裝excel");
return;
}
xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;
Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
//將ListView的列名導(dǎo)入Excel表第一行
foreach (ColumnHeader dc in listView.Columns)
{
columnIndex++;
xlApp.Cells[rowIndex, columnIndex] = dc.Text;
}
//將ListView中的數(shù)據(jù)導(dǎo)入Excel中
for (int i = 0; i < rowNum; i++)
{
rowIndex++;
columnIndex = 0;
for (int j = 0; j < columnNum; j++)
{
columnIndex++;
//注意這個在導(dǎo)出的時(shí)候加了“\t” 的目的就是避免導(dǎo)出的數(shù)據(jù)顯示為科學(xué)計(jì)數(shù)法。可以放在每行的首尾。
xlApp.Cells[rowIndex, columnIndex] = Convert.ToString(listView.Items[i].SubItems[j].Text) + "\t";
}
}
//例外需要說明的是用strFileName,Excel.XlFileFormat.xlExcel9795保存方式時(shí) 當(dāng)你的Excel版本不是95、97 而是2003、2007 時(shí)導(dǎo)出的時(shí)候會報(bào)一個錯誤:異常來自 HRESULT:0x800A03EC。 解決辦法就是換成strFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal。
xlBook.SaveAs(strFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlApp = null;
xlBook = null;
MessageBox.Show("OK");
}
}
您可能感興趣的文章:
- C#使用NPOI將List數(shù)據(jù)導(dǎo)出到Excel文檔
- C#通過NPOI導(dǎo)入導(dǎo)出數(shù)據(jù)EXCEL
- C#用NPOI導(dǎo)出導(dǎo)入Excel幫助類
- C#使用NPOI實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出功能
- C#使用NPOI導(dǎo)入Excel的方法詳解
- c#將Excel數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的實(shí)現(xiàn)代碼
- C#數(shù)據(jù)導(dǎo)入/導(dǎo)出Excel文件及winForm導(dǎo)出Execl總結(jié)
- C#導(dǎo)入導(dǎo)出EXCEL文件的代碼實(shí)例
- C# Winform實(shí)現(xiàn)導(dǎo)入和導(dǎo)出Excel文件
- C#使用NPOI將excel導(dǎo)入到list的方法
相關(guān)文章
基于C#實(shí)現(xiàn)的屏幕指定區(qū)域截屏代碼
這篇文章主要介紹了C#實(shí)現(xiàn)的屏幕指定區(qū)域截屏代碼,有需要的朋友可以參考一下2014-01-01
C#實(shí)現(xiàn)根據(jù)實(shí)體類自動創(chuàng)建數(shù)據(jù)庫表
本文主要介紹了C#通過自定義特性實(shí)現(xiàn)根據(jù)實(shí)體類自動創(chuàng)建數(shù)據(jù)庫表的方法。具有很好的參考價(jià)值,需要的朋友一起來看下吧2016-12-12
c#實(shí)現(xiàn)將pdf轉(zhuǎn)文本的示例分享
這篇文章主要介紹了c#實(shí)現(xiàn)將pdf轉(zhuǎn)文本的示例,需要的朋友可以參考下2014-03-03
WPF實(shí)現(xiàn)平面三角形3D運(yùn)動效果
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)平面三角形3D運(yùn)動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
C#使用CallContext緩存線程數(shù)據(jù)
這篇文章介紹了C#使用CallContext緩存線程數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05

