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

C# WinForm创建Excel文件的实例

程序员文章站 2022-09-04 09:47:59
实例如下: private bool createxcel(string xlsfile) { try {...

实例如下:

private bool createxcel(string xlsfile)
    {
      try
      {
        object missing = system.reflection.missing.value;
        excel.applicationclass myexcel = new excel.applicationclass();

        excel._workbook xbk;        //工作薄 
        excel._worksheet xst;   //工作sheet  

        xbk = myexcel.workbooks.add(true);

        for (int i = 0; i < 1; i++)
        {
          xst = (excel._worksheet)xbk.activesheet;
          xst.name = "sheet" + i;
          myexcel.sheets.add(missing, missing, 1, excel.xlsheettype.xlworksheet);
        }
        //myexcel.visible = true;
        if (xlsfile.tolower().endswith(".xlsx"))
        {
          xbk.saveas(xlsfile, 56, missing,
          missing, missing, missing, excel.xlsaveasaccessmode.xlshared,
          missing, missing, missing, missing, missing);
        }
        else
        {
          xbk.saveas(xlsfile, missing, missing,
          missing, missing, missing, excel.xlsaveasaccessmode.xlshared,
          missing, missing, missing, missing, missing);
        }
        myexcel.quit();
        return true;
      }
      catch (exception ex)
      {
        messagebox.show("excel创建失败!原因:" + ex.message);
        return false;
      }
    }

以上这篇c# winform创建excel文件的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。