一个EXCEL中有N个sheet,现在需要用C#代码,将其拆分为N个EXCEL,每个EXCEL只有一个sheet。 |
|
领导就是牛,问题解决了。代码如下,希望可以帮到后来人:
string excelpath = @"D:\xxx\xxx.xls"; Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); //app.Visible = true; object missing = Type.Missing; Microsoft.Office.Interop.Excel.Workbook workBook = app.Workbooks.Open(excelpath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); try { if (workBook != null) { for (int i = 1; i < workBook.Sheets.Count; i++) { Microsoft.Office.Interop.Excel.Worksheet _wSheets = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Worksheets[i]; Microsoft.Office.Interop.Excel.Workbook newBook = app.Workbooks.Add(missing); Worksheet mySheet = newBook.Sheets[1] as Worksheet; try { string filename = @"D:\temp" + _wSheets.Name + ".xls"; if (File.Exists(filename)) { File.Delete(filename); } //mySheet.Name = _wSheets.Name; _wSheets.Copy(mySheet, missing); newBook.SaveAs(filename, missing , missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, missing , missing, missing, missing, missing); } catch (Exception ex) { } finally { _wSheets = null; mySheet = null; newBook.Close(); } } } } catch (Exception ex) { } finally { //newBook.Close(); workBook.Close(); app.Quit(); app = null; } |
|
大家随便回答点什么,我要结贴给分了。
|
|
40分 |
谢谢楼主分享
|
收藏,学习。。。
|