【发布时间】:2015-06-07 05:03:09
【问题描述】:
我正在使用互操作来创建 Excel 表并将我的数据表存储在其中并为用户打开它。有些列我不希望用户编辑。我可以在导出 Excel 工作表时禁用特定单元格吗?
以下是我的代码
Excel.Workbook excelWorkBook = excelApp.Workbooks.Open(Server.MapPath("UploadedExcel/" + lblTestCode.Text.Trim() + ".xls"));
FieldInfo myf = typeof(TEST).GetField("abcd");
foreach (DataTable table in ds.Tables)
{
//Add a new worksheet to workbook with the Datatable name
Excel.Worksheet excelWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelWorkBook.Sheets.Add();
// excelWorkSheet.Name = "Sheet1";
for (int i = 1; i < table.Columns.Count + 1; i++)
{
excelWorkSheet.Cells[1, i] = table.Columns[i - 1].ColumnName;
}
for (int j = 0; j < table.Rows.Count; j++)
{
for (int k = 0; k < table.Columns.Count; k++)
{
excelWorkSheet.Cells[j + 2, k + 1] = table.Rows[j].ItemArray[k].ToString();
}
}
}
excelWorkBook.Save();
excelWorkBook.Close();
excelApp.Quit();
【问题讨论】:
标签: c# asp.net excel excel-interop