我在C#中创建了一个代码来创建一个excel文件。这是代码
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkBook.CheckCompatibility = false;
xlWorkBook.DoNotPromptForConvert = true;
xlApp.Visible = true; // ensure that the excel app is visible.
xlWorkSheet = (Excel.Worksheet)xlApp.ActiveSheet; // Get the current active worksheet.
Microsoft.Office.Interop.Excel.Worksheet worksheet2 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2); //Get more work sheet if neccessary
xlWorkSheet.Cells[3, 1] = "Name";
xlWorkBook.SaveAs(filepath + fileName, 51, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlShared, misValue, misValue, misValue, misValue, misValue); //For excel 2007 and above
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
现在,问题是当我再次打开这个文件时,我看到" Merge&中心"选项保持禁用状态所以,当我通过C#代码打开这个文件时,我无法合并单元格。我怎样才能做到这一点?
由于
答案 0 :(得分:1)
这是因为您使用Excel.XlSaveAsAccessMode.xlShared
。这适用于许多限制,请参阅here。请改用Excel.XlSaveAsAccessMode.xlNoChange
:
xlWorkBook.SaveAs(filepath + fileName, 51, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlNoChange, misValue, misValue, misValue, misValue, misValue);