我在C#中創建了一個創建excel文件的代碼。下面是代碼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();
現在的問題是,當我再次打開該文件,然後我看到「合併&中心」選項保持禁用。所以,當我通過C#代碼打開這個文件時,我無法合併單元格。我怎麼能做到這一點?
感謝
我建議您使用EPPlus庫進行excel數據操作,它易於使用且工作更快。你可以下載它[在這裏](https://epplus.codeplex.com/releases/view/89923)EPPlus的合併示例描述[這裏](http://stackoverflow.com/questions/6172488/merge-cells-using -epplus) – Fragment 2015-02-09 08:26:56
'new Excel.ApplicationClass();'?沒有'新的Excel.Application();'? – dovid 2015-02-09 08:34:55