C#在线预览文件

来源:互联网 发布:three.js 工具 编辑:程序博客网 时间:2024/05/20 03:38

  //paf在线预览功能        public  void Priview(System.Web.UI.Page p, string inFilePath)        {            p.Response.ContentType = "Application/pdf";            string fileName = inFilePath.Substring(inFilePath.LastIndexOf('\\') + 1);            p.Response.AddHeader("content-disposition", "filename=" + fileName);            p.Response.WriteFile(inFilePath);            p.Response.End();        }//调用      protected void Button1_Click(object sender, EventArgs e)        {            string files = "DemoFiles/aa.pdf";            pdf.Priview(this.Page, files);        }//  在线预览Excel====================public  void Priview(System.Web.UI.Page p, string inFilePath, string outDirPath = "")    {        Microsoft.Office.Interop.Excel.Application excel = null;        Microsoft.Office.Interop.Excel.Workbook xls = null;        excel = new Microsoft.Office.Interop.Excel.Application();        object missing = Type.Missing;        object trueObject = true;        excel.Visible = false;        excel.DisplayAlerts = false;        string randomName = DateTime.Now.Ticks.ToString();  //output fileName        xls = excel.Workbooks.Open(inFilePath, missing, trueObject, missing,                                    missing, missing, missing, missing, missing, missing, missing, missing,                                    missing, missing, missing);        //Save Excel to Html        object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;        Workbook wsCurrent = xls;//(Workbook)wsEnumerator.Current;        String outputFile = outDirPath + randomName + ".html";        wsCurrent.SaveAs(outputFile, format, missing, missing, missing,                          missing, XlSaveAsAccessMode.xlNoChange, missing,                          missing, missing, missing, missing);        excel.Quit();        //Open generated Html        Process process = new Process();        process.StartInfo.UseShellExecute = true;        process.StartInfo.FileName = outputFile;        process.Start();    }//调用string outputDirPath = @"D:\文件\"; //Word和Excel转换成Html,Html文件存放的位置//DemoFiles是项目下一级目录  protected void btnExcel_Click(object sender, EventArgs e)    {        string filePath = Server.MapPath("~") + @"\DemoFiles\20170428沌口片区社区资料.xlsx";        ExcelPreview.Priview(this, filePath, outputDirPath);    }


原创粉丝点击