欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-14 08:13:16
本文实例讲述了c#使用winform简单导出excel的方法。分享给大家供大家参考,具体如下: using excel; 在项目中引入excel.d...

本文实例讲述了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#程序设计有所帮助。