private void ExportExcelModel(List<NViewModel> list)
{
System.Windows.Forms.FolderBrowserDialog dir = new System.Windows.Forms.FolderBrowserDialog();
dir.ShowDialog();
string dirPath = dir.SelectedPath; if (dirPath != string.Empty)
{
//Excel文件路径
string xlsDir = SysConfig.PublicClass.GetTemplatePath();
//这里是文件列表,每个文件只包含一个sheet,想把多个类型一样的Excel文件合成一个Excel文件多个sheet
//mList : 物品档案 物品属性.xls
// 物品类别.xls
// 物品.xls
Dictionary<string, List<string>> mList = LoadModelList(list);
foreach (KeyValuePair<string, List<string>> item in mList)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的机器未安装Excel");
return;
}
Microsoft.Office.Interop.Excel.Workbook myBook = xlApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet sheet = null;
Microsoft.Office.Interop.Excel.Application srcXlApp = new Microsoft.Office.Interop.Excel.Application();
//获取原有的模板
foreach (string xlsName in item.Value)
{
//sheet是往前插入的,排序反着排
if (sheet != null)
{
myBook.Sheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
sheet = (Microsoft.Office.Interop.Excel.Worksheet)myBook.Worksheets[1];
sheet.Name = xlsName.Replace(".xls", string.Empty);
myBook.Save();
Microsoft.Office.Interop.Excel.Workbook srcWB = srcXlApp.Workbooks.Open(xlsDir + System.IO.Path.DirectorySeparatorChar + xlsName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//得到WorkSheet对象
Microsoft.Office.Interop.Excel.Worksheet srcWS = (Microsoft.Office.Interop.Excel.Worksheet)srcWB.Worksheets[1]; srcWS.Copy(Type.Missing, myBook.Sheets[1]);程序到这里就出错,异常来自HRESULT:0x800A03EC
srcWB.Close(false, Type.Missing, Type.Missing);
srcWS = null;
srcWB = null;
}
srcXlApp.Quit();
srcXlApp = null;
myBook.SaveAs(dirPath + System.IO.Path.DirectorySeparatorChar + item.Key + ".xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
myBook.Saved = true;
sheet = null;
myBook = null;
xlApp.Quit();
xlApp = null;
}
}
}
{
System.Windows.Forms.FolderBrowserDialog dir = new System.Windows.Forms.FolderBrowserDialog();
dir.ShowDialog();
string dirPath = dir.SelectedPath; if (dirPath != string.Empty)
{
//Excel文件路径
string xlsDir = SysConfig.PublicClass.GetTemplatePath();
//这里是文件列表,每个文件只包含一个sheet,想把多个类型一样的Excel文件合成一个Excel文件多个sheet
//mList : 物品档案 物品属性.xls
// 物品类别.xls
// 物品.xls
Dictionary<string, List<string>> mList = LoadModelList(list);
foreach (KeyValuePair<string, List<string>> item in mList)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的机器未安装Excel");
return;
}
Microsoft.Office.Interop.Excel.Workbook myBook = xlApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet sheet = null;
Microsoft.Office.Interop.Excel.Application srcXlApp = new Microsoft.Office.Interop.Excel.Application();
//获取原有的模板
foreach (string xlsName in item.Value)
{
//sheet是往前插入的,排序反着排
if (sheet != null)
{
myBook.Sheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
sheet = (Microsoft.Office.Interop.Excel.Worksheet)myBook.Worksheets[1];
sheet.Name = xlsName.Replace(".xls", string.Empty);
myBook.Save();
Microsoft.Office.Interop.Excel.Workbook srcWB = srcXlApp.Workbooks.Open(xlsDir + System.IO.Path.DirectorySeparatorChar + xlsName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//得到WorkSheet对象
Microsoft.Office.Interop.Excel.Worksheet srcWS = (Microsoft.Office.Interop.Excel.Worksheet)srcWB.Worksheets[1]; srcWS.Copy(Type.Missing, myBook.Sheets[1]);程序到这里就出错,异常来自HRESULT:0x800A03EC
srcWB.Close(false, Type.Missing, Type.Missing);
srcWS = null;
srcWB = null;
}
srcXlApp.Quit();
srcXlApp = null;
myBook.SaveAs(dirPath + System.IO.Path.DirectorySeparatorChar + item.Key + ".xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
myBook.Saved = true;
sheet = null;
myBook = null;
xlApp.Quit();
xlApp = null;
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货