【问题标题】:How to create Excel 2003 (.XLS) file with password protection without Excel installed with c#?如何在没有使用 c# 安装 Excel 的情况下创建具有密码保护的 Excel 2003 (.XLS) 文件?
【发布时间】:2014-01-29 13:13:41
【问题描述】:

在 C# 中,如何在没有安装 Excel 的情况下创建 PASSWORD PROTECTED(用户必须输入密码才能打开文件).XLS 文件(因此不是互操作)? NPOI 和 ExcelLibrary 看起来很有希望(因为它们是免费的!),但我似乎无法在任何地方找到它们是否真的支持密码保护。我不能使用 EPPlus,因为它只处理 .XSLX 文件类型,而不是我需要的 .XLS。

另外,我想使用一个数组来填充数据,而不是一个单元一个单元。这是我在过去使用 Interop 时所做的,它比逐个单元格的方法快得多:

object[,] data = new object[length, ColumnHeaders.Count];
...
dynamic rg = excelApp.Sheets[p].Range[excelApp.Sheets[p].Cells[top, left], excelApp.Sheets[p].Cells[bottom, right]];
rg.Value = data;

【问题讨论】:

  • +1 回答一个有趣的问题...我原以为答案是否定的,但我很想看看你是否得到任何答复。

标签: c# wpf excel passwords


【解决方案1】:

检查源代码示例: http://forums.asp.net/t/1831409.aspx

您还可以按数据集填充数据,而不是逐个单元格。

【讨论】:

  • 感谢您的回答。看起来 EasyXLS 是一个试用版,所以你必须购买该产品。
【解决方案2】:

导出代码

 public void ExportNewPatientsToExcel()
    {
        logger.Info("New Patients :: export to excel");
        string fileDirectory = string.Empty;

        if (Session[Constants.SESSION_FILE_DIRECTORY] != null)
            fileDirectory = Session[Constants.SESSION_FILE_DIRECTORY].ToString();
        else
        {
            logger.Error("New Patients::File Cache folder is not set.");
            Response.Redirect(Constants.PAGE_ERROR);
        }

        HttpContext context = HttpContext.Current;

        try
        {           
            string xsltFileName = Context.Server.MapPath(Constants.NEW_PATIENTS_XSLT_FILE_NAME);
            PatientCollection patientCollection = PatientBAO.GetNewPatients(ShowAllPatient);

            if (patientCollection.Count > 0 && patientCollection != null)
            {
                string fileName = PatientBAO.GenerateNewPatientsAsExcel(fileDirectory, xsltFileName, patientCollection);
                logger.Info("New Patients Excel version saved name :" + fileName);
                string fileNamePart = fileName.Substring(fileName.LastIndexOf("\\") + 1);
                fileNamePart = fileNamePart.Substring(fileNamePart.IndexOf("_") + 1);//added to remove session id from file name

                context.Items.Add(Constants.ENABLE_CACHE_SZ, Constants.ENABLE_CACHE);
                context.Response.ClearContent();
                context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileNamePart);
                context.Response.ContentType = "application/octet-stream";
                context.Response.TransmitFile(fileName);                
            }
            else
            {
                ShowPopUp(Resources.Patient.RecordNotFoundToExportExcel);
                logger.Error("New patients data not found for export to excel.");
            }
        }
        catch (Exception exc)
        {
            logger.ErrorException("Error occured while export patient details to excel.", exc);
        }
        finally
        {
            //HttpContext.Current.ApplicationInstance.CompleteRequest();
            context.Response.End();
        }
    }

密码部分正在处理。

【讨论】:

  • 知道密码部分后告诉我
猜你喜欢
  • 2010-09-14
  • 2021-10-07
  • 1970-01-01
  • 2012-09-11
  • 1970-01-01
  • 1970-01-01
  • 2021-02-23
  • 1970-01-01
  • 2014-06-18
相关资源
最近更新 更多