Asp.Net 常用工具类之Office-文档操作(6)
文档一直是老大难问题,君不知,代码用时方恨少!有一套成熟的文件帮助类能很大程度上减少寻找各种资料的时间。
记得以前做一个业务,需要导出协议,一份可编辑,一份不可编辑。那么Word和PDF是最好的选择,Word可编辑,PDF只给客户方面看!
前两天复习了一下Excel的操作,主要运用到NPOI组件。
今天给大家带来的是另一个组件Spire.DataExport的使用,主要是进行HTML,Word,PDF的导出操作。
第一次用到Spire.DataExport还是去年上半年的时候。说实话,非常强大,特别是针对于文档的转换和导入导出操作,非常便利,且使用简单,大家有兴趣的可以去官网查看相关资料,Demo非常清晰明了。
DataTable转Word
/// <summary> /// DataTable导出到Word /// </summary> /// <param name="filename">文件名称和路径</param> /// <param name="dt">dt</param> /// <param name="isweb">是否web导出</param> public static void DataTableToWord(string filename, DataTable dt, bool isweb = true) { if (dt != null && dt.Rows.Count > 0) { if (filename.IndexOf(".doc", StringComparison.OrdinalIgnoreCase) < 0 && filename.IndexOf(".docx", StringComparison.OrdinalIgnoreCase) < 0) filename += ".doc"; RTFExport word = new RTFExport { DataSource = Spire.DataExport.Common.ExportSource.DataTable, DataTable = dt, ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView }; using (MemoryStream ms = new MemoryStream()) { word.SaveToStream(ms); if (!isweb) { if (!filename.Contains("\\")) filename = Config.Config.GenerateFilePath("Office") + filename; if (!File.Exists(filename)) { using (FileStream fs = new FileStream(filename, FileMode.CreateNew)) { fs.Write(ms.ToArray(), 0, ms.ToArray().Length); fs.Close(); } } } else { HttpContext curContext = HttpContext.Current; curContext.Response.ContentType = "application/vnd.ms-word"; curContext.Response.ContentEncoding = Encoding.UTF8; curContext.Response.Charset = ""; curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8)); curContext.Response.BinaryWrite(ms.GetBuffer()); curContext.Response.End(); } } } }
DataTable转PDF
/// <summary> /// DataTable导出到PDF /// </summary> /// <param name="filename">文件名称和路径</param> /// <param name="dt">dt</param> /// <param name="isweb">是否web导出</param> public static void DataTableToPDF(string filename, DataTable dt, bool isweb = true) { if (dt != null && dt.Rows.Count > 0) { if (filename.IndexOf(".pdf", StringComparison.OrdinalIgnoreCase) < 0) filename += ".pdf"; Spire.DataExport.PDF.PDFExport pdf = new Spire.DataExport.PDF.PDFExport { DataSource = Spire.DataExport.Common.ExportSource.DataTable, DataTable = dt, ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView }; using (MemoryStream ms = new MemoryStream()) { pdf.SaveToStream(ms); if (!isweb) { if (!filename.Contains("\\")) filename = Config.Config.GenerateFilePath("Office") + filename; if (!File.Exists(filename)) { using (FileStream fs = new FileStream(filename, FileMode.CreateNew)) { fs.Write(ms.ToArray(), 0, ms.ToArray().Length); fs.Close(); } } } else { HttpContext curContext = HttpContext.Current; curContext.Response.ContentType = "application/pdf"; curContext.Response.ContentEncoding = Encoding.UTF8; curContext.Response.Charset = ""; curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8)); curContext.Response.BinaryWrite(ms.GetBuffer()); curContext.Response.End(); } } }
DataTable转HTML
/// <summary> /// DataTable导出到HTML /// </summary> /// <param name="filename">文件名称和路径</param> /// <param name="dt">dt</param> /// <param name="isweb">是否web导出</param> public static void DataTableToHTML(string filename, DataTable dt, bool isweb = true) { if (dt != null && dt.Rows.Count > 0) { if (filename.IndexOf(".html", StringComparison.OrdinalIgnoreCase) < 0) filename += ".html"; HTMLExport html = new HTMLExport { ActionAfterExport = ActionType.None, HtmlStyle = HtmlStyle.MSMoney, DataSource = ExportSource.DataTable, DataTable = dt }; using (MemoryStream ms = new MemoryStream()) { html.SaveToStream(ms); if (!isweb) { if (!filename.Contains("\\")) filename = Config.Config.GenerateFilePath("Office") + filename; if (!File.Exists(filename)) { using (FileStream fs = new FileStream(filename, FileMode.CreateNew)) { fs.Write(ms.ToArray(), 0, ms.ToArray().Length); fs.Close(); } } } else { HttpContext curContext = HttpContext.Current; curContext.Response.ContentType = "text/html"; curContext.Response.ContentEncoding = Encoding.UTF8; curContext.Response.Charset = ""; curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8)); curContext.Response.BinaryWrite(ms.GetBuffer()); curContext.Response.End(); } } } }
OK,各位看官,近期的文档操作写到这里喏,感谢大家的支持,您的支持是我的动力!
下一期给大家带来的是Config文件操作,敬请期待!!!