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

C#應用winform簡略導出Excel的辦法

編輯:C#入門知識

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


本文實例講述了C#應用winform簡略導出Excel的辦法。分享給年夜家供年夜家參考,詳細以下:

using Excel;

在項目中引入Excel.dll

/// <summary>
/// 導出Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExportExcel_Click(object sender, EventArgs e)
{
  DataTable dt = this.dgvWaterTicket.DataSource;
  if (dt == null)
  {
    return;
  }
  if (dt.Rows.Count == 0)
  {
    return;
  }
  Excel.Application xlApp = new Excel.Application();
  if (xlApp == null)
  {
    MessageBox.Show("請確保您的電腦曾經裝置Excel", "提醒信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
  }
  xlApp.UserControl = true;
  Excel.Workbooks workbooks = xlApp.Workbooks;
  //依據模版發生新的workbook //Workbook workbook = workbooks.Add("D:\\aa.xls");
  Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
  Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//獲得sheet1
  if (worksheet == null)
  {
    MessageBox.Show("請確保您的電腦曾經裝置Excel", "提醒信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
  }
  try
  {
    Excel.Range range;
    long totalCount = dt.Rows.Count;
    long rowRead = 0;
    float percent = 0;
    worksheet.Cells[1, 1] = frm.Text;//導出的題目
    worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, dt.]).MergeCells = true; //歸並單位格---列數
    worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 3]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//居中對齊
    worksheet.get_Range(worksheet.Cells[1, 3], worksheet.Cells[1, 3]).ColumnWidth = 15;   //列寬
    worksheet.get_Range(worksheet.Cells[1, 2], worksheet.Cells[1, 2]).ColumnWidth = 15;   //列寬
    worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 1]).ColumnWidth = 20;   //列寬
    //寫入字段
    for (int i = 0; i < dt.Columns.Count; i++)
    {
      worksheet.Cells[2, i + 1] = dt.Columns[i].ColumnName;
      range = (Excel.Range)worksheet.Cells[2, i + 1];
      range.Interior.ColorIndex = 15;
      range.Font.Bold = true;
    }
    //寫入數值
    for (int r = 0; r < dt.Rows.Count; r++)
    {
      for (int i = 0; i < dt.Columns.Count; i++)
      {
        worksheet.Cells[r + 3, i + 1] = dt.Rows[r][i];
      }
      rowRead++;
      percent = ((float)(100 * rowRead)) / totalCount;
      //System.Threading.Thread.Sleep(500);
      //假如字的數目過量則主動換行。worksheet.Cells[r+1, 4]為worksheet.Cells[行, 列]
      worksheet.get_Range(worksheet.Cells[r + 3, 4], worksheet.Cells[r + 1, 4]).Columns.WrapText = true;   //主動換行
      worksheet.get_Range(worksheet.Cells[r + 3, 4], worksheet.Cells[r + 3, 4]).Rows.AutoFit(); //主動加行高
      //this.Text = "導出數據[" + percent.ToString("0.00") + "%]...";
    }
    range = worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[dt.Rows.Count + 2, dt.Columns.Count]);
    range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
    range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
    range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlContinuous;
    range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
    if (dt.Columns.Count > 1)
    {
      range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
      range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
      range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
    }
    xlApp.Visible = true;
  }
  catch
  {
    MessageBox.Show("到出Excel掉敗!", "提醒信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
  }
  finally
  {
    System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
    //KillProcess("Excel");
    GC.Collect();//強行燒毀
  }
}

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

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