用c#保存一个excel文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8875120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:55:06  来源:igfitidea点击:

Save an excel file in c#

c#excel

提问by happysmile

  void excelsave()
  {
      try
      {
          ApplicationClass app = new ApplicationClass(); // the Excel application.

          Workbook book = null;
          Worksheet sheet = null;
          Range range = null;
          // the range object is used to hold the data
          app.Visible = false;
          app.ScreenUpdating = false;
          app.DisplayAlerts = false;

          string execPath =
            Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);

          book = app.Workbooks.Open(@"E:\SSIS\ABC\Book1.xls",
               Missing.Value, Missing.Value, Missing.Value,
               Missing.Value, Missing.Value, Missing.Value, Missing.Value,
               Missing.Value, Missing.Value, Missing.Value, Missing.Value,
               Missing.Value, Missing.Value, Missing.Value);
          sheet = (Worksheet)book.Worksheets[1];

          range = sheet.get_Range("A1", Missing.Value);
          range.Columns.ColumnWidth = 22.34;
          range = sheet.get_Range("B1", Missing.Value);
          range.Columns.ColumnWidth = 22.34;
          book.SaveAs(@"E:\SSIS\ABC\Book1.xls", Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
       }
       catch (Exception ex)
       {

       }
 }

Here I am opening an excel sheet trying to increase the column width and need to make the column headers as boldand save the document, right now the document is not getting saved. I am using vs 2008, c# 3.5

在这里,我正在打开一个 excel 表,试图增加列宽,需要将列标题设为粗体并保存文档,现在文档没有被保存。我正在使用 vs 2008,c# 3.5

Is There anything that I am doing wrong here? any help on this would be great looking an for solution

我在这里做错了什么吗?对此的任何帮助都会很好地寻找解决方案

回答by Edward Leno

I ran the following using VS 2010 and .NET 4, but this code should still work in your environment. Also, I simplified your code a bit. Hopefully this will get you going in the right direction.

我使用 VS 2010 和 .NET 4 运行了以下代码,但此代码仍可在您的环境中使用。另外,我稍微简化了您的代码。希望这会让你朝着正确的方向前进。

    static void excelsave()
    {
        try
        {
            Application app = new Application();
            string execPath =
              Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);

            Workbook book = app.Workbooks.Open(@"c:\test.xls");
            Worksheet sheet = (Worksheet)book.Worksheets[1];

            Range range = sheet.get_Range("A1");
            range.Columns.ColumnWidth = 22.34;
            range = sheet.get_Range("B1");
            range.Columns.ColumnWidth = 22.34;

            sheet.get_Range("A1", "B1").Font.Bold = true;

            book.SaveAs(@"c:\test2.xls");  // or book.Save();
            book.Close();
        }
        catch (Exception ex)
        {
        }
    } 

UPDATE

更新

You can find a similar explanation/example of what you are doing at: http://www.dotnetperls.com/excel

您可以在以下网址找到类似的解释/示例:http: //www.dotnetperls.com/excel

Marshal.ReleaseComObject(book);  // do this after the close

Also, there is a good discussion on cleaning up Excel/COM ... How To Properly Clean Up Excel Interop Objects In c#

另外,有一个关于清理 Excel/COM 的很好的讨论...... How To Properly Clean Up Excel Interop Objects In c#