C#使用winform簡單匯出Excel的方法

來源:互聯網
上載者:User
本文執行個體講述了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#使用winform簡單匯出Excel的方法相關文章請關注topic.alibabacloud.com!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.