本方法是针对word导出操作,需要制作好的模板文件 模板.doc
引入应用Microsoft.Office.Interop.Word 11.0 (office2003)
导出文件注意:有时候迅雷会在浏览器中安装插件,下载时会默认使用迅雷下载,导致下载的文档格式丢失,这时为了避免错误,可以将下载方法DownFile()写在另一个页面中,通过session将参数传递过去就可以
1 /// <summary> 2 /// 导出按钮 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 protected void ImageButton2_Click(object sender, ImageClickEventArgs e) 7 { 8 9 string id; 10 id = Session["ObjChangeID"].ToString(); 11 try 12 { 13 14 15 if (id!=null) 16 { 17 18 string _filePath = PrintDoc(id); 19 if (_filePath != "") 20 { 21 DownloadFile(_filePath, Path.GetFileName(_filePath));//下载文件 22 } 23 } 24 } 25 catch (Exception ex) 26 { 27 ZWL.Common.PublicMethod.errorLog("ibtnExport_Click", ex); 28 } 29 } 30 /// <summary> 31 /// 打印操作,传入车辆Carcod或者变更ID 32 /// </summary> 33 /// <param name="id"></param> 34 protected string PrintDoc(string id) 35 { 36 Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application(); 37 Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document(); 38 try 39 { 40 41 if (id.Length != 36) 42 { 43 objCarInfor.OpenCar(id); 44 string templeteName = "模版.doc", downName = "";///模板文件名称 45 downName = objCarInfor.ORG_NAME + "新文件.doc";//导出文件名 46 47 string templeteFile = System.Web.HttpContext.Current.Server.MapPath("~/") + "DocTempalete\" + templeteName;//模板文件全路径 48 string downFile = System.Web.HttpContext.Current.Server.MapPath("~/") + "ReportFile\gonghan\" + downName;///导出文件全路径 49 try 50 { 51 File.Delete(downFile);//删除原有的同名文件 52 } 53 catch 54 { 55 } 56 File.Copy(templeteFile, downFile);//复制模板文件到导出文件对应的文件夹下存档 57 object Obj_FileName = downFile; 58 object Visible = false; 59 object ReadOnly = false; 60 object missing = System.Reflection.Missing.Value; 61 //打开文件 62 doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing, 63 ref missing, ref missing, ref missing, ref missing, 64 ref missing, ref missing, ref missing, ref Visible, 65 ref missing, ref missing, ref missing, 66 ref missing); 67 doc.Activate(); 68 #region 给模板填入类容 69 //光标转到书签 70 object BookMarkName = "函号"; 71 object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark; 72 73 74 if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true) 75 { 76 doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName); 77 doc.ActiveWindow.Selection.TypeText(System.DateTime.Now.ToString("yyMMdd")); 78 } 79 BookMarkName = "年"; 233 what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark; 234 235 236 if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true) 237 { 238 doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName); 239 doc.ActiveWindow.Selection.TypeText(DateTime.Now.ToString("yy")); 240 } 241 BookMarkName = "月"; 242 what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark; 243 244 245 if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true) 246 { 247 doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName); 248 doc.ActiveWindow.Selection.TypeText(DateTime.Now.ToString("MM")); 249 } 250 BookMarkName = "日"; 251 what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark; 252 253 254 if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true) 255 { 256 doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName); 257 doc.ActiveWindow.Selection.TypeText(DateTime.Now.ToString("dd")); 258 } ///注意:书签必须不一样才能真确绑定,如果说模板中有需要出现两次的的内容,必须设置成两个标签:
/// 如:姓名显示两次,则必须给两个位置都加上标签,以示区别
259 #endregion 260 object IsSave = true; 261 doc.Close(ref IsSave, ref missing, ref missing);///关闭doc文档对象 262 System.Runtime.InteropServices.Marshal.ReleaseComObject(doc); 263 264 doc = null; 265 266 object IsSave1 = false; 267 app.Quit(ref IsSave1, ref missing, ref missing); 268 System.Runtime.InteropServices.Marshal.ReleaseComObject(app); 269 app = null; 270 GC.Collect(); 271 return downFile; 272 } 273 else 274 { 275 return ""; 276 } 277 278 } 279 catch (Exception ex) 280 { 281 app = null; 282 GC.Collect(); 283 return ""; 284 } 285 286 } 287 /// 288 /// <summary> 289 /// 下载文件 290 /// </summary> 291 /// <param name="filename">文件名(全路径)</param> 292 /// <param name="downname">文件下载名</param> 293 protected void DownloadFile(string filename, string downname) 294 { 295 FileStream f; 296 byte[] buffer = new byte[0]; 297 try 298 { 299 f = new FileStream(filename, FileMode.Open); 300 buffer = new byte[f.Length]; 301 f.Read(buffer, 0, buffer.Length); 302 f.Close(); 303 } 304 catch 305 { 306 ZWL.Common.MessageBox.Show(this, "文件不存在!"); 307 return; 308 } 309 310 filename = filename.Replace(@"/", @""); 311 //20121023wangyj 312 313 string saveFileName = ""; 314 int intStart = filename.LastIndexOf("\") + 1; 315 saveFileName = filename.Substring(intStart, filename.Length - intStart); 316 317 Response.Clear(); 318 Response.Buffer = true; 319 Response.Charset = "GB2312"; 320 321 string fileType = Path.GetExtension(filename).ToLower(); 322 323 switch (fileType) 324 { 325 case ".asf": 326 System.Web.HttpContext.Current.Response.ContentType = "video/x-ms-asf"; 327 break; 328 case ".jpg": 329 case ".jpeg": 330 System.Web.HttpContext.Current.Response.ContentType = "image/jpeg"; 331 break; 332 case ".gif": 333 System.Web.HttpContext.Current.Response.ContentType = "image/gif"; 334 break; 335 case ".pdf": 336 System.Web.HttpContext.Current.Response.ContentType = "application/pdf"; 337 break; 338 case ".avi": 339 System.Web.HttpContext.Current.Response.ContentType = "video/avi"; 340 break; 341 case ".doc": 342 System.Web.HttpContext.Current.Response.ContentType = "application/msword"; 343 break; 344 case ".zip": 345 System.Web.HttpContext.Current.Response.ContentType = "application/zip"; 346 break; 347 case ".rar": 348 System.Web.HttpContext.Current.Response.ContentType = "application/rar"; 349 break; 350 case ".xls": 351 System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; 352 break; 353 case ".wav": 354 System.Web.HttpContext.Current.Response.ContentType = "audio/wav"; 355 break; 356 case ".mp3": 357 System.Web.HttpContext.Current.Response.ContentType = "audio/mpeg3"; 358 break; 359 case ".mpg": 360 System.Web.HttpContext.Current.Response.ContentType = "audio/mpeg"; 361 break; 362 case ".rtf": 363 System.Web.HttpContext.Current.Response.ContentType = "application/rtf"; 364 break; 365 case ".htm": 366 case ".html": 367 System.Web.HttpContext.Current.Response.ContentType = "text/html"; 368 break; 369 case ".asp": 370 System.Web.HttpContext.Current.Response.ContentType = "text/asp"; 371 break; 372 default: 373 System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream"; 374 break; 375 } 376 377 Response.HeaderEncoding = System.Text.Encoding.GetEncoding("GB2312"); 378 Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(downname)); 379 Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); 380 381 //Response.WriteFile(System.Configuration.ConfigurationSettings.AppSettings["TemplatePhysicalPath"].ToString() + pt.Path); 382 Response.BinaryWrite(buffer); 383 Response.GetHashCode(); 384 Response.End(); 385 }