程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#導出數據到Excel文件的辦法

C#導出數據到Excel文件的辦法

編輯:C#入門知識

C#導出數據到Excel文件的辦法。本站提示廣大學習愛好者:(C#導出數據到Excel文件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#導出數據到Excel文件的辦法正文


本文實例講述了C#導出數據到Excel文件的辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:

/// <summary>
/// 導出到Excel類,項目需援用Microsodt.Office.Interop.Excel,
/// 類文件需using System.Data與System.Windows.Forms定名空間
/// </summary>
public class CToExcel
{
  /// <summary>
  /// 導出到Excel
  /// </summary>
  /// <param name="fileName">默許文件名</param>
  /// <param name="listView">數據源,一個頁面上的ListView控件</param>
  /// <param name="titleRowCount">題目占領的行數,為0表現無題目</param>
  public void ExportExcel(string fileName, System.Windows.Forms.ListView listView,int titleRowCount)
  {
   string saveFileName = "";
   //bool fileSaved = false;
   SaveFileDialog saveDialog = new SaveFileDialog();
   saveDialog.DefaultExt = "xls";
   saveDialog.Filter = "Excel文件|*.xls";
   saveDialog.FileName = fileName;
   saveDialog.ShowDialog();
   saveFileName = saveDialog.FileName;
   if (saveFileName.IndexOf(":") < 0) return; //被點了撤消 
   Microsoft.Office.Interop.Excel.Application xlApp;
   try
   {
    xlApp = new Microsoft.Office.Interop.Excel.Application();
   }
   catch (Exception)
   {
    MessageBox.Show("沒法創立Excel對象,能夠您的機子未裝置Excel");
    return;
   }
   finally
   {
   }
   Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
   Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
   Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//獲得sheet1
   //寫Title
   if(titleRowCount!=0)
    MergeCells(worksheet, 1, 1, titleRowCount, listView.Columns.Count, listView.Tag.ToString());
   //寫出列題目
   for (int i = 0; i <= listView.Columns.Count - 1; i++)
   {
    worksheet.Cells[titleRowCount+1, i + 1] = listView.Columns[i].Text;
   }
   //寫入數值
   for (int r = 0; r <= listView.Items.Count - 1; r++)
   {
    for (int i = 0; i <= listView.Columns.Count - 1; i++)
    {
     worksheet.Cells[r + titleRowCount+2, i + 1] = listView.Items[r].SubItems[i].Text;
    }
    System.Windows.Forms.Application.DoEvents();
   }
   worksheet.Columns.EntireColumn.AutoFit();//列寬自順應
   //if (Microsoft.Office.Interop.cmbxType.Text != "Notification")
   //{
   // Excel.Range rg = worksheet.get_Range(worksheet.Cells[2, 2], worksheet.Cells[ds.Tables[0].Rows.Count + 1, 2]);
   // rg.NumberFormat = "00000000";
   //}
   if (saveFileName != "")
   {
    try
    {
     workbook.Saved = true;
     workbook.SaveCopyAs(saveFileName);
     //fileSaved = true;
    }
    catch (Exception ex)
    {
     //fileSaved = false;
     MessageBox.Show("導出文件時失足,文件能夠正被翻開!n" + ex.Message);
    }
   }
   //else
   //{
   // fileSaved = false;
   //}
   xlApp.Quit();
   GC.Collect();//強行燒毀 
   // if (fileSaved && System.IO.File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName); //翻開EXCEL
   MessageBox.Show(fileName + "導出到Excel勝利", "提醒", MessageBoxButtons.OK);
  }
  /// <summary>
  /// DataTable導出到Excel
  /// </summary>
  /// <param name="fileName">默許的文件名</param>
  /// <param name="dataTable">數據源,一個DataTable數據表</param>
  /// <param name="titleRowCount">題目占領的行數,為0則表現無題目</param>
  public void ExportExcel(string fileName,System.Data.DataTable dataTable,int titleRowCount)
  {
   string saveFileName = "";
   //bool fileSaved = false;
   SaveFileDialog saveDialog = new SaveFileDialog();
   saveDialog.DefaultExt = "xls";
   saveDialog.Filter = "Excel文件|*.xls";
   saveDialog.FileName = fileName;
   saveDialog.ShowDialog();
   saveFileName = saveDialog.FileName;
   if (saveFileName.IndexOf(":") < 0) return; //被點了撤消 
   Microsoft.Office.Interop.Excel.Application xlApp;
   try
   {
    xlApp = new Microsoft.Office.Interop.Excel.Application();
   }
   catch (Exception)
   {
    MessageBox.Show("沒法創立Excel對象,能夠您的機子未裝置Excel");
    return;
   }
   finally
   {
   }
   Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
   Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
   Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//獲得sheet1
   //寫Title
   if(titleRowCount!=0)
    MergeCells(worksheet, 1, 1, titleRowCount, dataTable.Columns.Count, dataTable.TableName);
   //寫出列題目
   for (int i = 0; i <= dataTable.Columns.Count - 1; i++)
   {
    worksheet.Cells[titleRowCount+1, i + 1] = dataTable.Columns[i].ColumnName;
   }
   //寫入數值
   for (int r = 0; r <= dataTable.Rows.Count - 1; r++)
   {
    for (int i = 0; i <= dataTable.Columns.Count - 1; i++)
    {
     worksheet.Cells[r +titleRowCount+ 2, i + 1] = dataTable.Rows[r][i].ToString();
    }
    System.Windows.Forms.Application.DoEvents();
   }
   worksheet.Columns.EntireColumn.AutoFit();//列寬自順應
   //if (Microsoft.Office.Interop.cmbxType.Text != "Notification")
   //{
   // Excel.Range rg = worksheet.get_Range(worksheet.Cells[2, 2], worksheet.Cells[ds.Tables[0].Rows.Count + 1, 2]);
   // rg.NumberFormat = "00000000";
   //}
   if (saveFileName != "")
   {
    try
    {
     workbook.Saved = true;
     workbook.SaveCopyAs(saveFileName);
     //fileSaved = true;
    }
    catch (Exception ex)
    {
     //fileSaved = false;
     MessageBox.Show("導出文件時失足,文件能夠正被翻開!n" + ex.Message);
    }
   }
   //else
   //{
   // fileSaved = false;
   //}
   xlApp.Quit();
   GC.Collect();//強行燒毀 
   // if (fileSaved && System.IO.File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName); //翻開EXCEL
   MessageBox.Show(fileName + "導出到Excel勝利", "提醒", MessageBoxButtons.OK);
  }
  /// <summary> 
  /// 歸並單位格,並賦值,對指定WorkSheet操作 
  /// </summary> 
  /// <param name="sheetIndex">WorkSheet索引</param> 
  /// <param name="beginRowIndex">開端行索引</param> 
  /// <param name="beginColumnIndex">開端列索引</param> 
  /// <param name="endRowIndex">停止行索引</param> 
  /// <param name="endColumnIndex">停止列索引</param> 
  /// <param name="text">歸並後Range的值</param> 
  public void MergeCells(Microsoft.Office.Interop.Excel.Worksheet workSheet, int beginRowIndex, int beginColumnIndex, int endRowIndex, int endColumnIndex, string text)
  {
   Microsoft.Office.Interop.Excel.Range range = workSheet.get_Range(workSheet.Cells[beginRowIndex, beginColumnIndex], workSheet.Cells[endRowIndex, endColumnIndex]);
   range.ClearContents(); //先把Range內容消除,歸並才不會失足 
   range.MergeCells = true;
   range.Value2 = text;
   range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
   range.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
  } 
}

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved