【问题标题】:ReportViewer export to Excel with passwordReportViewer 使用密码导出到 Excel
【发布时间】:2015-09-21 08:56:07
【问题描述】:

我在 MVC 中有一个代码可以成功导出 Excel 文件,但是如何将密码添加到 Excel 文件中?

我的代码:

public ActionResult Report(string id)

        {

            LocalReport lr = new LocalReport();
            string path = Path.Combine(Server.MapPath("~/Report"), "ReportStateArea.rdlc");
            if (System.IO.File.Exists(path))
            {
                lr.ReportPath = path;
            }
            else
            {
                return View("Index");
            }
            List<StateArea> cm = new List<StateArea>();
            using (myDatabaseEntities dc = new myDatabaseEntities())
            {
                cm = dc.StateAreas.ToList();
            }
            ReportDataSource rd = new ReportDataSource("MyDataset", cm);
            lr.DataSources.Add(rd);
            string reportType = id ;
            string mimeType;
            string encoding;
            string fileNameExtension;



            string deviceInfo =

            "<DeviceInfo>" +
            "  <OutputFormat>" + id + "</OutputFormat>" +
            "  <PageWidth>8.5in</PageWidth>" +
            "  <PageHeight>11in</PageHeight>" +
            "  <MarginTop>0.5in</MarginTop>" +
            "  <MarginLeft>1in</MarginLeft>" +
            "  <MarginRight>1in</MarginRight>" +
            "  <MarginBottom>0.5in</MarginBottom>" +
            "</DeviceInfo>";

            Warning[] warnings;

            string[] streams;
            byte[] renderedBytes;

            renderedBytes = lr.Render(
                reportType,
                deviceInfo,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings);





            return File(renderedBytes, mimeType);
        }

【问题讨论】:

    标签: excel model-view-controller passwords export reportviewer


    【解决方案1】:

    您可以使用FileStream Class 在文件上写入renderedBytes

    Dim strExcelFile As String = "C:\YourPathTo\ExcelFile.xls"
    
    Dim fs As New FileStream(strExcelFile, FileMode.Create)
    fs.Write(renderedBytes, 0, renderedBytes.Length)
    fs.Close()
    fs.Dispose()
    

    然后使用Microsoft.Office.Interop.Excel.dll 为现有工作簿添加密码:

    Dim appExcel As Excel.Application
    Dim wbExcel As Excel.Workbook
    
    appExcel = New Excel.Application
    wbExcel = appExcel.Workbooks.Open(strExcelFile)
    
    wbExcel.Password = "YourPassword"
    
    appExcel.DisplayAlerts = False
    wbExcel.SaveAs(strExcelFile)
    appExcel.DisplayAlerts = True
    
    wbExcel.Close()
    

    我使用DisplayAlerts 来抑制提示和警报消息,同时使用SaveAs 覆盖现有文件;当消息需要响应时,Microsoft Excel 会选择默认响应。

    我的代码在 VB.NET 中,但您可以简单地用 C# 翻译它。

    【讨论】:

    • 但是当我创建它是 byte[] 然后只返回到文件。一旦返回查看,它将开始下载。我能做什么?
    • 这样我就知道了路径。请帮忙
    • 未测试,但我认为您需要将文件保存在虚拟路径上(使用 FileStream 时使用 MapPath 获取物理文件路径),修改文件添加密码然后将此虚拟路径返回到客户端而不是返回 File(renderedBytes, mimeType)。
    • 我可以提供退货的示例代码吗?对不起,因为我是初学者。谢谢
    • 您也可以重新转换成字节密码保护的excel文件并继续使用return File(excelPasswordProtectedBytes, mimeType)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 2020-08-04
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多