gpt4 book ai didi

c# - 将数据插入到excel C#中的特定单元格

转载 作者:行者123 更新时间:2023-11-30 14:29:46 27 4
gpt4 key购买 nike

我一直在四处寻找如何将数据插入/更新到现有 Excel 电子表格的特定单元格中。我第一次看到这个线程:

Insert text in specific cell in Excel c#

不确定我是否没看错,但我没有看到任何关于使用某个 Excel 文件的信息。然后我会使用这种代码的组合吗?

http://csharp.net-informations.com/excel/csharp-excel-oledb-update.htm

抱歉,如果我只是使用链接,我不想用太多代码使这个线程困惑。

最佳答案

我没有浏览过您在帖子中提到的链接。但是,如果您要写入或更新特定单元格中的值,则可以使用以下代码行:

workSheet.Cells[rowIndex, colIndex] = cellValue;

rowIndexcolumnIndex 是整数值,表示您尝试写入/更新的单元格的行和列的索引。 cellValue 是您尝试在单元格中写入的值。这应该是一个字符串。

示例代码:

//Create an excel application object
Excel.Application excelAppObj = new Excel.Application();
excelAppObj.DisplayAlerts = false;

//Open the excel work book
Excel.Workbook workBook = excelAppObj.Workbooks.Open(fileName, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, false, false);

//Get the first sheet of the selected work book
Excel.Worksheet worksheet = (Excel.Worksheet)workBook.Worksheets.get_Item(1);

//Write 20 in Cell - C3
workSheet.Cells[2, 3] = "20";

//Save work book (.xlsx format)
workBook.SaveAs(fileName, Excel.XlFileFormat.xlOpenXMLWorkbook, null, null, false,
false, Excel.XlSaveAsAccessMode.xlShared, false, false, null, null, null);

//Release resources

关于c# - 将数据插入到excel C#中的特定单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25170741/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com