简体   繁体   English

在C#中保存Excel文件

[英]Save an excel file in c#

  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 bold and save the document, right now the document is not getting saved. 在这里,我正在打开一个excel表,尝试增加列宽,并且需要将列标题设置为粗体并保存文档,现在文档尚未保存。 I am using vs 2008, c# 3.5 我正在使用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 任何对此的帮助将是一个不错的解决方案

I ran the following using VS 2010 and .NET 4, but this code should still work in your environment. 我使用VS 2010和.NET 4进行了以下运行,但是此代码仍应在您的环境中运行。 Also, I simplified your code a bit. 另外,我简化了您的代码。 Hopefully this will get you going in the right direction. 希望这将使您朝正确的方向前进。

    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方面也有很好的讨论。 如何正确清理c#中的Excel互操作对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM