/// <summary> /// 将DataTable到出到Excel文件中(重载1) /// </summary> /// <param name="strTargetPath">文件地址</param> /// <param name="strFileName">文件明</param> /// <param name="dt">待导出的DataTable对象</param> /// <returns>true:success/false:faile</returns> public static bool ExportExcelFile(DataTable dt, out string strOut) { strOut = ""; bool flag = false; try { ////判断参数有效性 //if (strFileName.Trim().CompareTo("") == 0 || dt == null || dt.Rows.Count == 0) //{ // return false; //} //判断参数有效性 if (dt == null || dt.Rows.Count == 0) { return(false); } //创建excel对象 Excel.Application excelMatching = new Excel.Application(); excelMatching.Workbooks.Add(true);//创建excel工作薄 //把数据表的各个信息输入到excel表中 for (int i = 0; i < dt.Rows.Count + 1; i++) { for (int j = 0; j < dt.Columns.Count; j++) { int temp = j + 1; if (i == 0) { if (dt.Columns[j].ColumnName.ToString() == ("F" + temp.ToString())) { continue; } excelMatching.Cells[i + 1, j + 1] = dt.Columns[j].ColumnName.ToString(); } else { if (dt.Columns[j].ColumnName.ToString() == ("F" + temp.ToString())) { continue; } excelMatching.Cells[i + 1, j + 1] = dt.Rows[i - 1][j].ToString(); } } } //使excel可见 //excelMatching.Visible = true; //excelMatching.Save(strTargetPath + "\\" + strFileName); //excelMatching.Save("data"); //保存 //excelMatching.Workbooks[1].SaveCopyAs(strTargetPath + "\\" + strFileName); //excelMatching.Workbooks[1].Saved = false; //excelMatching.UserControl = false; //string strSaveFileName = (string)excelMatching.GetSaveAsFilename("a", "Excel文档(*.xls)|*.xls", 0, "b", "c"); excelMatching.Workbooks[1].Saved = true; excelMatching.UserControl = false; strOut = (string)excelMatching.GetSaveAsFilename("计划导入失败列表", "excel (*.xls), *.xls", 0, "保存", "保存"); //excelMatching.Save(strOut); excelMatching.Workbooks[1].SaveCopyAs(strOut); excelMatching.Quit(); flag = true; } catch { throw; } return(flag); }