C#导出excel,固定模板问题

kamYee 2013-12-11 02:31:43
现在要导出一个excel,但是模板是之前做好的,我想吧数据直接追加在excel后面,格式还是按照模板格式,
...全文
401 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ade150 2016-02-25
  • 打赏
  • 举报
回复
怎么实现能教一下吗?我刚好也碰到了
F12578626 2015-03-08
  • 打赏
  • 举报
回复
楼主是怎么解决的呢?我现在也碰到了这个问题
kamYee 2014-02-24
  • 打赏
  • 举报
回复
谢谢大家,我自己找到解决办法了
  • 打赏
  • 举报
回复
“按照模板格式”,也就是说模板的格式改变了那么输出的结果也就改变。 一定不是写死什么 oSheet.Cells[2, 5] 之类的。
terrence1106 2013-12-15
  • 打赏
  • 举报
回复
希望对你有用 using System.Reflection; //MISSIN using Microsoft.Office.Interop.Excel; //APP using System.Diagnostics; using System.IO; using System.Xml; using System.Data.SqlClient; public void outputExcel(System.Data.DataTable dataTable, string templateName) { string url = ""; //获取template文件夹路经,UserExcel.aspx和template文件夹在同一父目录下 string Path = this.GetFileFolder("OutExcel.aspx") + "template//"; string XmlPath = Path; //使用excel Application app = new Application(); Process[] pro = Process.GetProcessesByName("EXCEL"); int proID = pro[pro.Length - 1].Id; app.DisplayAlerts = false; try { //打开模板 string fileName = XmlPath + templateName + ".xlt"; object template_p = fileName; object newTemplate_p = false; object visible_p = true; object updateLink_p = Missing.Value; object ReadOnly_p = false; object Format_p = Missing.Value; object password = ""; object Writepassword = ""; object Recommended_p = Missing.Value; object Origin_p = 2; object Delimitit_p = Missing.Value; object Editable_p = true; object Notify_p = Missing.Value; object Converter_p = Missing.Value; object AddToMru_p = Missing.Value; object local_p = Missing.Value; object corruptload_p = Missing.Value; Microsoft.Office.Interop.Excel._Workbook wc = app.Workbooks._Open(fileName, updateLink_p, ReadOnly_p, Format_p, password, Writepassword, Recommended_p, Origin_p, Delimitit_p, Editable_p, Notify_p, Converter_p, AddToMru_p); //定位sheet _Worksheet oSheet = (_Worksheet)wc.Worksheets[1]; switch (templateName) { case "TraficAcciStatMonthReport": oSheet.Cells[2, 5] = dataTable.Rows[0]["ReportMonth"].ToString();// 报表月份 //oSheet.Cells[7, 2] = dataTable.Rows[0]["AccidentNum"].ToString();// 本月事故起数 //oSheet.Cells[8, 2] = dataTable.Rows[0]["AccidentSum"].ToString();// 本月事故起数 oSheet.Cells[7, 2] = dataTable.Rows[0]["DeathAcciNum"].ToString();// 本月事故起数 oSheet.Cells[8, 2] = dataTable.Rows[0]["DeathAcciSum"].ToString(); oSheet.Cells[7, 3] = dataTable.Rows[0]["PreDeathAcciNum"].ToString(); oSheet.Cells[8, 3] = dataTable.Rows[0]["PreDeathAcciSum"].ToString(); break; } //templateName //保存文件 System.Random rand = new System.Random(); int a = rand.Next(); string ss = a.ToString(); string pathStr; pathStr = XmlPath.Remove(XmlPath.Length - 1, 1); int position = pathStr.LastIndexOf("\\"); pathStr = pathStr.Remove(position + 1, pathStr.Length - position - 1); pathStr = pathStr + "trush\\"; object fileName_p = pathStr + ss + ".xls"; ; object fileFormat_p = Missing.Value; object password_p = Missing.Value; object writePassword_p = Missing.Value; XlSaveAsAccessMode readOnlyRecommended_p = new XlSaveAsAccessMode(); object CreateBackup_p = Missing.Value; Microsoft.Office.Interop.Excel.XlSaveAsAccessMode accessmode_p = new XlSaveAsAccessMode(); object conflictResolutuin = Missing.Value; object addtoMru_p = Missing.Value; object Textcode_p = Missing.Value; object textVisuall_p = Missing.Value; wc._SaveAs(fileName_p, fileFormat_p, password, writePassword_p, readOnlyRecommended_p, CreateBackup_p, accessmode_p, conflictResolutuin, addtoMru_p, Textcode_p, textVisuall_p); ///关闭模板 object saveChanges_p = Missing.Value; object originalFormat_p = Missing.Value; object routeDocument_p = Missing.Value; wc.Close(saveChanges_p, originalFormat_p, routeDocument_p); app.Quit(); app = null; wc = null; if (proID != 0) KillallTimer(proID); //url = this.Request.Url.ToString();// ;//.AbsoluteUri; url = this.Request.Url.AbsoluteUri; url = url.Substring(0, url.Length - "OutExcel.aspx".Length) + "trush\\"; //url = url + "trush\\"; url = url + ss + ".xls"; Response.Write(@"<script language=javascript>window.close();window.open('" + url.Replace("\\", "/") + "')</script>"); } catch (Exception ex) { Response.Write(ex.ToString()); if (proID != 0) KillallTimer(proID); } } private void KillallTimer(int ProcessID) { try { GC.Collect(); System.Diagnostics.Process[] pw = System.Diagnostics.Process.GetProcessesByName("WINWORD"); for (int i = 0; i < pw.Length; i++) { System.IntPtr c = pw[i].MainWindowHandle; if (c.ToInt32() == 0) { if (pw[i].Id == ProcessID) { pw[i].Kill(); } } } GC.Collect(); } catch { GC.Collect(); } }
  • 打赏
  • 举报
回复
什么“模板格式”?
kamYee 2013-12-15
  • 打赏
  • 举报
回复
自己顶
kamYee 2013-12-11
  • 打赏
  • 举报
回复
没人嘛

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧