C# 复制公式--并将其应用于新的单元格区域
这就是我所经历的。我正在使用带c的Excel dll,以便进入一个大而讨厌的Excel工作表中,以便其他人不必这样做 我们在一个单元格中有一个相当大的公式,因此我们不想将它复制到每一行。此公式在其所在行上使用多个值。如果它位于第1行,它将使用该行中的许多单元格 在excel中正常复制此公式时,将修改单元格的新范围以反映新的起始位置 问题是,当我像这样复制公式时,它仍然会给出与第一行有关的所有值,而不是与粘贴它的行有关的所有值……以下是我的代码:C# 复制公式--并将其应用于新的单元格区域,c#,excel,C#,Excel,这就是我所经历的。我正在使用带c的Excel dll,以便进入一个大而讨厌的Excel工作表中,以便其他人不必这样做 我们在一个单元格中有一个相当大的公式,因此我们不想将它复制到每一行。此公式在其所在行上使用多个值。如果它位于第1行,它将使用该行中的许多单元格 在excel中正常复制此公式时,将修改单元格的新范围以反映新的起始位置 问题是,当我像这样复制公式时,它仍然会给出与第一行有关的所有值,而不是与粘贴它的行有关的所有值……以下是我的代码: sheet.Cells[77][row].Fo
sheet.Cells[77][row].Formula = sheet.Cells[77][1].Formula;
有人能告诉我如何使公式实际应用于新行而不是第1行吗?这可能会起作用,因为它在VBA中起作用。。。在大多数情况下
sheet.Cells[77][row].FormulaR1C1 = sheet.Cells[77][1].FormulaR1C1;
这将起作用,因为没有一个非常有用的链接使用R1C1表示法来描述引用的单元格相对于当前单元格的位置,而不是说明要使用哪些单元格。这意味着实际引用取决于带有公式的单元格。当您仅使用公式时,您复制的公式字符串正好包括硬编码的单元格引用。这可能会起作用,因为它在VBA中工作。。。在大多数情况下
sheet.Cells[77][row].FormulaR1C1 = sheet.Cells[77][1].FormulaR1C1;
这将起作用,因为没有一个非常有用的链接使用R1C1表示法来描述引用的单元格相对于当前单元格的位置,而不是说明要使用哪些单元格。这意味着实际引用取决于带有公式的单元格。当您仅使用公式时,您复制的公式字符串正好包括硬编码的单元格引用。您可以使用Application.ConvertFormula
假设我的Cell=Cells有一个公式,表示同一行的C77细胞=SumB77。
如果要将其复制到其正下方的单元格中,可以执行以下操作:
string formula = Sheet1.Cells[77][2].Formula;
Sheet1.Cells[77][2].Formula = app.ConvertFormula(formula, XlReferenceStyle.xlA1, XlReferenceStyle.xlR1C1, XlReferenceType.xlRelative, Sheet1.Cells[77][3]);
完整的控制台应用程序,但你需要修改单元格
static void Main(string[] args)
{
var app = new Microsoft.Office.Interop.Excel.Application();
var workbook = app.Workbooks.Open(@"C:\Users\user\Desktop\Book1.xlsx");
Microsoft.Office.Interop.Excel.Worksheet Sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item("Sheet1");
string formula = Sheet1.Cells[5][3].Formula;
Sheet1.Cells[5][4].Formula = app.ConvertFormula(formula, XlReferenceStyle.xlA1, XlReferenceStyle.xlR1C1, XlReferenceType.xlRelative, Sheet1.Cells[5][3]);
workbook.SaveAs(@"C:\Users\user\desktop\test.xlsx");
workbook.Close();
}
您可以根据自己的喜好修改ConvertFormula方法的第三个和第四个参数。请在此处阅读有关该方法的详细信息:。
如果要跨多行拉伸公式帐户,可以尝试使用Application.ConvertFormula
假设我的Cell=Cells有一个公式,表示同一行的C77细胞=SumB77。
如果要将其复制到其正下方的单元格中,可以执行以下操作:
string formula = Sheet1.Cells[77][2].Formula;
Sheet1.Cells[77][2].Formula = app.ConvertFormula(formula, XlReferenceStyle.xlA1, XlReferenceStyle.xlR1C1, XlReferenceType.xlRelative, Sheet1.Cells[77][3]);
完整的控制台应用程序,但你需要修改单元格
static void Main(string[] args)
{
var app = new Microsoft.Office.Interop.Excel.Application();
var workbook = app.Workbooks.Open(@"C:\Users\user\Desktop\Book1.xlsx");
Microsoft.Office.Interop.Excel.Worksheet Sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item("Sheet1");
string formula = Sheet1.Cells[5][3].Formula;
Sheet1.Cells[5][4].Formula = app.ConvertFormula(formula, XlReferenceStyle.xlA1, XlReferenceStyle.xlR1C1, XlReferenceType.xlRelative, Sheet1.Cells[5][3]);
workbook.SaveAs(@"C:\Users\user\desktop\test.xlsx");
workbook.Close();
}
您可以根据自己的喜好修改ConvertFormula方法的第三个和第四个参数。请在此处阅读有关该方法的详细信息:。
如果要跨多行拉伸公式帐户,可以尝试使用您好,因为此代码用于复制Excel中单元格后面的公式:
public void copy_Formula_behind_cell()
{
Excel.Application xlapp;
Excel.Workbook xlworkbook;
Excel.Worksheet xlworksheet;
Excel.Range xlrng;
object misValue = System.Reflection.Missing.Value;
xlapp = new Excel.Application();
xlworkbook =xlapp.Workbooks.Open("YOUR_FILE", 0, true, 5, "",
"",true,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t",
false,
false, 0, true, 1, 0);
xlworksheet = (Excel.Worksheet)xlworkbook.Worksheets.get_Item(1);
string sp = xlworksheet.Cells[3,2].Formula;//It will Select Formula using Fromula method//
xlworksheet.Cells[8,2].Formula =
xlapp.ConvertFormula(sp,XlReferenceStyle.xlA1,
XlReferenceStyle.xlR1C1, XlReferenceType.xlAbsolute,
xlworksheet.Cells[8][2]);
//This is used to Copy the exact formula to where you want//
xlapp.Visible = true;
xlworkbook.Close(true, misValue, misValue);
xlapp.Quit();
releaseObject(xlworksheet);
releaseObject(xlworkbook);
releaseObject(xlapp);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
大家好,我发布此消息是因为此代码用于复制Excel中单元格后面的公式:
public void copy_Formula_behind_cell()
{
Excel.Application xlapp;
Excel.Workbook xlworkbook;
Excel.Worksheet xlworksheet;
Excel.Range xlrng;
object misValue = System.Reflection.Missing.Value;
xlapp = new Excel.Application();
xlworkbook =xlapp.Workbooks.Open("YOUR_FILE", 0, true, 5, "",
"",true,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t",
false,
false, 0, true, 1, 0);
xlworksheet = (Excel.Worksheet)xlworkbook.Worksheets.get_Item(1);
string sp = xlworksheet.Cells[3,2].Formula;//It will Select Formula using Fromula method//
xlworksheet.Cells[8,2].Formula =
xlapp.ConvertFormula(sp,XlReferenceStyle.xlA1,
XlReferenceStyle.xlR1C1, XlReferenceType.xlAbsolute,
xlworksheet.Cells[8][2]);
//This is used to Copy the exact formula to where you want//
xlapp.Visible = true;
xlworkbook.Close(true, misValue, misValue);
xlapp.Quit();
releaseObject(xlworksheet);
releaseObject(xlworkbook);
releaseObject(xlapp);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
我使用c代码和Microsoft.Office.Interop.excel库发布excel公式的以下代码:
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.Excel();
}
public void Excel()
{
Application xlApp = new Application();
Workbook xlWorkBook;
Worksheet xlWorkSheet;
object misValue = Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1);
for (int r = 1; r < 5; r++) //r stands for ExcelRow and c for ExcelColumn
{
// Its a my sample example: Excel row and column start positions for writing Row=1 and Col=1
for (int c = 1; c < 3; c++)
{
if (c == 2)
{
if (r == 1)
{
xlWorkSheet.Cells[r, c].Formula = "=SUM(A1+200)";
}
continue;
}
xlWorkSheet.Cells[r, c] = r;
}
}
Range rng = xlWorkSheet.get_Range("B1");
// This is the main code we can range our excel sheet formulas
rng.AutoFill(xlWorkSheet.get_Range("B1", "B4"), XlAutoFillType.xlLinearTrend);
xlWorkBook.Worksheets[1].Name = "MySheetData";//Renaming the Sheet1 to MySheet
xlWorkBook.SaveAs(@"E:\test.xlsx");
xlWorkBook.Close();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
}
}
我使用c代码和Microsoft.Office.Interop.excel库发布excel公式的以下代码:
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.Excel();
}
public void Excel()
{
Application xlApp = new Application();
Workbook xlWorkBook;
Worksheet xlWorkSheet;
object misValue = Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1);
for (int r = 1; r < 5; r++) //r stands for ExcelRow and c for ExcelColumn
{
// Its a my sample example: Excel row and column start positions for writing Row=1 and Col=1
for (int c = 1; c < 3; c++)
{
if (c == 2)
{
if (r == 1)
{
xlWorkSheet.Cells[r, c].Formula = "=SUM(A1+200)";
}
continue;
}
xlWorkSheet.Cells[r, c] = r;
}
}
Range rng = xlWorkSheet.get_Range("B1");
// This is the main code we can range our excel sheet formulas
rng.AutoFill(xlWorkSheet.get_Range("B1", "B4"), XlAutoFillType.xlLinearTrend);
xlWorkBook.Worksheets[1].Name = "MySheetData";//Renaming the Sheet1 to MySheet
xlWorkBook.SaveAs(@"E:\test.xlsx");
xlWorkBook.Close();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
}
}
为什么不直接将带有公式的单元格复制/粘贴到新的目的地呢?你能用c实现吗?这就是我想要的。@Mizmor,提供的任何解决方案对您有效吗?@TimWilliams复制/粘贴都可以,但如果您计划对许多单元格执行此操作,则会严重影响性能。Daniel Cooks的答案几乎是即时的。为什么不直接将带有公式的单元格复制/粘贴到新的目的地呢?你能用c实现吗?这就是我想要的。@Mizmor,提供的任何解决方案对您有效吗?@TimWilliams复制/粘贴都可以,但如果您计划对许多单元格执行此操作,则会严重影响性能。丹尼尔·库克的回答几乎是即时的。