C#在线预览文件

  //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);
    }


阅读更多
文章标签: C#
个人分类: c#
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭