欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

C#使用winform简单导出Excel的方法

来源:本站原创|时间:2020-01-10|栏目:C#教程|点击:

本文实例讲述了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#相关内容感兴趣的读者可查看本站专题:《C#操作Excel技巧总结》、《C#中XML文件操作技巧汇总》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#数据结构与算法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》

希望本文所述对大家C#程序设计有所帮助。

上一篇:如何解决hash冲突

栏    目:C#教程

下一篇:C#实现的ZPL条码打印类完整实例

本文标题:C#使用winform简单导出Excel的方法

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/6410.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有