我有一张excel表,其中包含以前带有一些格式的某些值。我想使用c#以编程方式添加新行。如何在插入操作期间将上一行的样式应用于此新添加的行?
我使用的代码如下:
Application excelApp = new Application();
Workbooks workBooks = excelApp.Workbooks;
Workbook workBook = excelApp.Workbooks.Open(fileName, Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
int nTotalSheets = workBook.Worksheets.Count;
Worksheet workSheet = workBook.Worksheets[1];
int nColumns = workSheet.UsedRange.Columns.Count;
int nRows = workSheet.UsedRange.Rows.Count;
workSheet.Cells[nRows + 1, 1] = "Test";
Range excelRange = workSheet.UsedRange;
workBook.Close(true, fileName, false);
Marshal.ReleaseComObject(workBook);
答案 0 :(得分:0)
找到答案here
我正在寻找以下代码:
workSheet.Cells[nRows + 1, 3] = DateTime.Today;
Range sourceRange = workSheet.Cells[nRows, 3];
sourceRange.Copy(Type.Missing);
Range destinationRange = workSheet.Cells[nRows + 1, 3];
destinationRange.PasteSpecial(XlPasteType.xlPasteFormats, XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
destinationRange.PasteSpecial(XlPasteType.xlPasteFormulas, XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
最后一行是复制公式。