我正在尝试以下代码,但有一个例外:
无法更改合并单元格的一部分。
在这一行抛出了异常:
PortfolioSheet.get_Range("A1",missing).EntireColumn.Locked = false;
完整代码:
Microsoft.Office.Interop.Excel.Application ThisApplication = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook ThisWorkBook;
string _stFileName = Server.MapPath("/ExcelTemplate/EFIRM_Facility_Template.xlsm");
object missing = System.Reflection.Missing.Value;
ThisWorkBook = ThisApplication.Workbooks.Open(_stFileName, missing, missing, 5, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing);
Microsoft.Office.Interop.Excel.Worksheet PortfolioSheet = (Microsoft.Office.Interop.Excel.Worksheet)ThisWorkBook.Sheets[1]; //fill portfoliosheet
//PortfolioSheet.get_Range("A1" + dtDynamicForPortfolio.Rows.Count + "").Locked = false;
/* This line */
PortfolioSheet.get_Range("A1",missing).EntireColumn.Locked = false;
PortfolioSheet.get_Range("B1", "H1"+dtDynamicForPortfolio.Rows.Count).Locked = false;
int iCol = 1;
int iRow = 0;
foreach (DataRow r in dtDynamicForPortfolio.Rows)
{
iRow++;
iCol = 0;
foreach (DataColumn c in dtDynamicForPortfolio.Columns)
{
iCol++;
PortfolioSheet.Cells[iRow + 1, iCol] = Convert.ToString(r[c.ColumnName]);
// ThisSheet.get_Range("A1", "C3").Locked = true;
}
}
PortfolioSheet.get_Range("A1", missing).EntireColumn.Locked = true;
PortfolioSheet.get_Range("B1", "H1").Locked = true;
我怎样才能避免这种情况?