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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值