【问题标题】:Microsoft.Office.Interop.Excel cells remain null after setting themMicrosoft.Office.Interop.Excel 单元格在设置后保持为空
【发布时间】:2020-02-12 04:07:56
【问题描述】:

我有一个正在尝试写入 Excel 文件的应用程序。它似乎不起作用。

这是我的代码:

using System;
using System.Drawing;
using Microsoft.Office.Interop.Excel;

namespace Image2Excel {
class Engine {
    public static void go(string imageFilename, string excelFilename = null) {
        //Image img = Image.FromFile(imageFilename);
        Bitmap btm = (Bitmap)Bitmap.FromFile(imageFilename, false);
        Image img = Image.FromFile(imageFilename, false);

        if (btm != null) {
            Color[][] colorArray = new Color[btm.Width][];
            for (int x = 0; x < btm.Width; x++) {
                colorArray[x] = new Color[btm.Height];
                for (int y = 0; y < btm.Height; y++) {
                    colorArray[x][y] = btm.GetPixel(x, y);
                }
            }

            var excel = new Microsoft.Office.Interop.Excel.Application();
            var workbook = excel.Workbooks.Add(Type.Missing);
            var worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
            worksheet.Name = "sheet1";

            worksheet.Cells[1,1] = "top left";
            worksheet.Cells[1,2] = "top right";
            worksheet.Cells[2,1] = "bottom left";
            worksheet.Cells[2,2] = "bottom right";

            workbook.SaveAs("temp.xlsx");
            workbook.Close();
            excel.Quit();

            Marshal.ReleaseComObject(worksheet);
            Marshal.ReleaseComObject(workbook);
            Marshal.ReleaseComObject(excel);
        }
    } 
}
}

我正在尝试在电子表格的前 4 个单元格中写入一些文本。它不工作。当我打开文件时,我在单元格中看不到任何内容。

我确实看到工作表名称设置为我设置的名称,因此我知道它正在写入文件。

我在调试时注意到的一件事是 worksheet.Cells[1,1] 在我设置后为空。我想知道在写入之前是否需要将单元格设置为对象。但我不确定如何。我似乎无法做到这一点:

worksheet.Cells[1,1] = new Microsoft.Office.Interop.Excel.Cell();

悬停在 Cells 上告诉我它是一个 Range 对象,但我也不知道如何将它设置为一个范围。我试过这个:

worksheet.Range["A1"].Value = "hello";
worksheet.Range["A1"].Value2 = "hello";

...但是那些也不起作用。 worksheet.Range 与 worksheet.Cells 一样保持为空。

如何确保 worksheet.Cells[1,1] 或 worksheet.Range["A1"] 设置为将采用字符串值的对象?谢谢。

你可以在 github 上找到我的应用程序:https://github.com/gibran-shah/Image2Excel

我使用的是 Microsoft.Office.Interop.Excel 版本 15.0.4795.1000。

我的电脑上安装了 Excel 2010。

【问题讨论】:

  • 试试 worksheet.Cells[1,1].Value = "top left";或 excel.Cells[1,1].Value = "左上角";
  • 肯定有其他事情发生。我复制/粘贴您拥有的代码,它似乎按预期工作。文本已成功保存在 Excel 文件中。您确定您查看的是正确的 Excel 文件吗?如果没有路径,文件将保存到项目的 bin 文件夹中。可能使用 excel.Visible = true; 设置 Excel 应用可见,并在保存和关闭之前设置一个断点。
  • 另外,您确定您使用的是正确的互操作版本吗?我猜 Excel 2010 使用版本 14。你是如何安装互操作的?我建议您删除当前的互操作版本,并简单地引用您计算机上的互操作版本。

标签: c# excel interop


【解决方案1】:

我没试过你的,但你能试试这些方法吗?它对我有用。

第一种方法Worksheets.Cells

// CREATE EXCEL OBJECTS.
Excel.Application xlApp;

Excel.Workbook xlWB;
Excel.Worksheet xlWS;

xlApp = new Excel.Application();

// Specify the path to the excel file
xlWB= xlApp.Workbooks.Open("path/to/xlFile.xls");

// Choose the sheet you want to open
xlWS = xlWB.Worksheets["Sheet1"];

// Write on the single cell 
xlWS.Cells["1", "D"] = "Example";

// Save the change and quit
xlWB.Close(true);
xlApp.Quit();           

第二个方法Worksheets.get_Range()

// CREATE EXCEL OBJECTS.
Excel.Application xlApp;

Excel.Workbook xlWB;
Excel.Worksheet xlWS;

xlApp = new Excel.Application();

// Specify the path to the excel file
xlWB= xlApp.Workbooks.Open("path/to/xlFile.xls");

// Choose the sheet you want to open
xlWS = xlWB.Worksheets["Sheet1"];

// Write on the range of cells 
xlWS.get_Range["A3", "D5"] = "Example";

// Save the change and quit
xlWB.Close(true);
xlApp.Quit();           

【讨论】:

    猜你喜欢
    • 2021-12-19
    • 2013-07-02
    • 2016-01-18
    • 2020-10-13
    • 2017-03-01
    • 2021-10-04
    • 1970-01-01
    • 2013-10-08
    • 2023-03-09
    相关资源
    最近更新 更多