【问题标题】:C# not creating the excel file and cannot write in different excel sheetsC#不创建excel文件,不能在不同的excel表中写入
【发布时间】:2015-08-28 12:43:38
【问题描述】:

首先,在我的代码中,我打开一个 Excel 表并将其读入列表。其次,我关闭了这个 excel 表并创建了一个新的 excel 表:

excel_init("C:\\Users\\oma\\Desktop\\excel2.xlsx"); // this path does NOT exist yet

接下来是这个方法:

static void excel_init(String path)
{
    appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();

    if (System.IO.File.Exists(path))
    {
        // then go and load this into excel
        newWorkbook_First = appExcel.Workbooks.Open(path, true, true);
        objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet;
    }
    else
    {
        try
        {
            appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
            appExcel.Visible = true;
            newWorkbook_First = appExcel.Workbooks.Add(1);
            objsheet = (Microsoft.Office.Interop.Excel.Worksheet)newWorkbook_First.Sheets[1];
            objsheet.Name = ("test");
            var newSheet3 = (Microsoft.Office.Interop.Excel.Worksheet)appExcel.Worksheets.Add(Type.Missing, appExcel.Worksheets[appExcel.Worksheets.Count], 1, XlSheetType.xlWorksheet);
            newSheet3.Name = "test2";
            var newSheet2 = (Microsoft.Office.Interop.Excel.Worksheet)appExcel.Worksheets.Add(Type.Missing, appExcel.Worksheets[appExcel.Worksheets.Count], 1, XlSheetType.xlWorksheet);
            newSheet2.Name = "test3";
        }
        catch (Exception e)
        {
            Console.Write("Error");
        }
        finally
        {
        }
    }
}

它不会在我的桌面上创建 excel2.xlsx,为什么不呢?

接下来我主要是这样喊 excel_setValue:

excel_setValue("C", "hello", "");

这是excel_setValue函数:

static void excel_setValue(string cellname, string value, string color)
{
    objsheet.get_Range(cellname).set_Value(Type.Missing, value);
    if (kleur == "red")
    {
        objsheet.get_Range(cellname).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
    }
}

如何将工作表添加到此?例如:excel_setValue(cellname, value, color, sheetname)

最后我做 excel_close();

excel_close 函数:

static void excel_close()
        {
            if (appExcel != null)
            {
                try
                {
                    newWorkbook_First.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel.ActiveWorkbook.ActiveSheet);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel.ActiveWorkbook);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
                    appExcel = null;
                    objsheet = null;
                }
                catch (Exception ex)
                {
                    appExcel = null;
                    Console.WriteLine("Unable to release the Object " + ex.ToString());
                }
                finally
                {
                    GC.Collect();
                }
            }
        }

它关闭并询问我是否要保存/不保存或取消它。并且它将保存@文档而没有给出错误

【问题讨论】:

  • "It wont create excel2.xlsx on my desktop, why not?" - 它以什么方式失败?有错误吗?它是在其他地方创建文件吗?请提供更多详细信息。
  • 没有给出错误,它关闭并告诉我是否要将文件保存在文档中,不保存文件或只是取消

标签: c# excel


【解决方案1】:

我创建这样的 Excel 对我来说效果很好!

您需要在 newWorkbook_First.close(); 部分之前的代码中执行以下操作:

 newWorkbook_First.SaveAs(totalPath);

我希望这能帮助你解决问题..

using Excel = Microsoft.Office.Interop.Excel;


internal static bool ExportDGV(DataGridView DGV, List<string> selectedCustomerList, string path, string fileName, string exportName, int exportType, string mailSubject)
{
    string totalPath;
    //Create an Excel application instance
    Excel.Application excelApp = new Excel.Application();
    Excel.Workbook excelWorkBook = excelApp.Application.Workbooks.Add();
    Excel._Worksheet worksheet = null;
    excelApp.Visible = false;
    worksheet = excelWorkBook.ActiveSheet;

    //set headers
    for (int i = 1; i < DataGridView.Columns.Count + 1; i++)
    {
    worksheet.Cells[1, i] = DGV.Columns[i - 1].HeaderText;
    }
    createList(worksheet, DGV);

    //Create excel with the choosen name
    Worksheet sheet1 = excelWorkBook.Worksheets[1];
    worksheet.Name = fileName;

    totalPath = path + "/" + fileName + ".xlsx";

    //if path exist add a number
    totalPath = directoryExist(totalPath, path, fileName);
    //Save exel and Quit exelApp 

    excelWorkBook.SaveAs(totalPath);
    excelWorkBook.Close();
    excelApp.Quit();
}

【讨论】:

    【解决方案2】:

    您可以将EpPlus 库用于excel。它非常易于使用并且有据可查。你不再需要使用 COM 类了

    【讨论】:

    • 这应该是对问题的评论,而不是答案。
    猜你喜欢
    • 1970-01-01
    • 2019-02-12
    • 2022-01-26
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多