Sample code sharing for creating Excel files using C#WinForm

黄舟
Release: 2017-03-27 11:12:21
Original
1736 people have browsed it

The following editor will bring you an example of C# WinForm creating Excel files. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

The examples are as follows:

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;
      }
    }
Copy after login

The above is the detailed content of Sample code sharing for creating Excel files using C#WinForm. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template