打开一个excel文件并添加单元格值
我为我的项目使用C#&Excel Intropt.dll
我想打开我的xlsx文件并向Sheet1中的所有单元格添加一个值(例如“1”)。
我怎么能做到这一点?
喜欢这个:
12 14 19 22 81 91 26 62
结果:
13 15 20 23 82 92 27 63
你可以使用这个片段。 单元格[1,1]是左上angular的第一个单元格。 这会得到第一个表格,但是您也可以通过名称来引用它们。
Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.ApplicationClass(); xlWorkBook = xlApp.Workbooks.Open(_filename, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); //You can loop through all cells and use i and j to get the cells xlWorkSheet.Cells[1,1].Value2 = Convert.ToInt32(xlWorkSheet.Cells[1,1].Value2) + 1; xlWorkBook.Close(false, misValue, misValue); xlApp.Quit();