(C# – Excel)如何将公式写入每个现有行的新创build列?

假设你有一个excel文件,包含大约100行的数字和行。

我想以编程方式编写一个公式列,为每个现有行添加一堆字段的值。

所以首先,我会为Excel文件创buildOpenfiledialog,findpath,通过OleDb打开它。 明白了这一点。 现在说我有下面的表格:

[ 朋友| Money | 钱丢失| 天过去了| 解决 ]

Bilbo,50,50,7,*这里公式

Bilso,80,50,7,*这里公式

等等…

问题是,我没有确切的行数,所以我必须枚举OleDb连接find的任何东西。 公式可能类似于=(B2-C2)* D2,但是2也是错误的,它必须是相对于它所在的任何行,即:第3行将是=(B3-C3)* D3。 我不知道如何做到这一点。

我发现直接写入单元格也不会使公式直接处理数字。

=====

编辑:

private Excel.Application Xls; private Excel.Workbooks WBs; private Excel.Workbook WB; private Excel.Worksheet WS; private Excel.Sheets SS; Excel.Range cellsRange = null; Excel.Range columnRange = null; Excel.Range rowRange = null; int numberOfColumns = 0; int numberOfRows = 0; private void btnColumn_Click(object sender, EventArgs e) { if (btnColumn.FlatStyle == FlatStyle.Flat) btnColumn.FlatStyle = FlatStyle.Standard; else { btnColumn.FlatStyle = FlatStyle.Flat; } OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Excel Files|*.xls;*.xlsx"; openFileDialog.ShowDialog(); string pather = openFileDialog.FileName; if (pather == "" || pather == " " || pather == null) { return; } if (!string.IsNullOrEmpty(openFileDialog.FileName)) { try { Xls = new Excel.Application(); WBs = Xls.Workbooks; WB = WBs.Open(pather, 0, false, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); SS = WB.Worksheets; WS = SS.get_Item(1); cellsRange = WS.Cells; columnRange = cellsRange.Columns; rowRange = cellsRange.Rows; numberOfColumns = columnRange.Count; numberOfRows = rowRange.Count; int LastCell = numberOfColumns+1; WS.Cells[1, LastCell] = "Tax Formula"; for (int i = 2; i < numberOfRows; i++) { //string niceFormula = "=SUM(L" + i + ",M" + i + ")*N"+ i ; //WS.Cells[i, LastCell].Formula = niceFormula; WS.Cells[i, LastCell].Value = (WS.Cells[i, 12] + WS.Cells[i, 13]) * WS.Cells[i, 14]; } //========================== WB.Save(); } catch (Exception ex) { MessageBox.Show("Write Excel: " + ex.Message); } finally { GC.Collect(); GC.WaitForPendingFinalizers(); WB.Close(); Xls.Quit(); releaseObject(cellsRange); releaseObject(columnRange); releaseObject(rowRange); releaseObject(SS); releaseObject(WS); releaseObject(WBs); releaseObject(WB); releaseObject(Xls); } MessageBox.Show("Finished Updating File", "Task complete"); } } 

任何人都知道为什么这个代码在写入尝试时抛出以下错误?

HRESULT:0x800A03EC

为了您的方便,重新整理了代码。 它仍然吐出HRESULT错误。

目标项目是4行深,1行标题和约16列宽。

编辑:

你可能想试试这个:

 numberOfColumns = WS.UsedRange.Columns.CountLarge; numberOfRows = WS.UsedRange.Rows.CountLarge; 

CountLarge ,如果使用CountLarge而不是Count ,也可以将它们改为long而不是int

好吧 – 我最终得到了这个工作 – 不知道为什么我有这么多问题。

对我来说,我显然必须使应用程序可见或不工作。

可能有另一个解决方法 – 我从这里拉: https : //stackoverflow.com/a/17061714/1274820

这段代码最后为我工作:

请注意,这是一个控制台应用程序(想我会排除),但你应该能够添加魔术线。

 Application excelApp = new Application(); excelApp.SheetsInNewWorkbook = 1; //////////////////////////////////////////// //Add these lines to make it work??? //////////////////////////////////////////// try { excelApp.Visible = true; } catch {} //////////////////////////////////////////// Workbook excelWB = excelApp.Workbooks.Open(@"C:\test.xls", Type.Missing, false); _Worksheet excelWS = excelWB.Sheets[1]; Range cellsRange = excelWS.UsedRange; long LastCell = cellsRange.Columns.CountLarge + 1; long numberOfRows = cellsRange.Rows.CountLarge; excelWS.Cells[1, LastCell] = "Tax Formula"; for (int i = 2; i <= numberOfRows; i++) { string niceFormula = "=SUM(L" + i + ",M" + i + ")*N" + i; excelWS.Cells[i, LastCell].Formula = niceFormula; } excelWB.Close(true); excelApp.Quit(); 

之前:

之前

后:

后

如何使用Excel电子表格填写DataTable:

 ////////////////////////////////////////////////////////////////////////////////// //This function is absolute magic >.> - Fill DataTable with excel spreadsheet //HDR=YES means "Spreadsheet has headers" Change to NO if not. //name = "Log"; - This is the Sheet name to pull the data from ////////////////////////////////////////////////////////////////////////////////// //oconn takes an SQL like command (Select Everything from name sheet) using con //HDR=YES means that our data has headers :) ////////////////////////////////////////////////////////////////////////////////// String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + copyToPath + ";Extended Properties='Excel 12.0 XML;HDR=YES;';"; OleDbConnection con = new OleDbConnection(constr); OleDbCommand oconn = new OleDbCommand("Select * From [Log$]", con); con.Open(); OleDbDataAdapter sda = new OleDbDataAdapter(oconn); DataTable data = new DataTable(); sda.Fill(data); con.Close(); ////////////////////////////////////////////////////////////////////////////////// 

为什么不使用R1C1参考风格? 这样你的公式不需要相对于列。

供参考: https : //excelmate.wordpress.com/2013/04/22/excel-r1c1-reference-style-vs-a1/