如何在不安装c#的情况下创建带有密码保护的Excel 2003 (.XLS)文件?

时间:2023-01-15 10:32:25

In C#, how can I create a PASSWORD PROTECTED (user has to enter password to OPEN the file at all) .XLS file without having Excel installed (therefore not Interop)? NPOI and ExcelLibrary look promising (because they are free!), but I cannot seem to find anywhere whether or not they actually support password protection. I cannot use EPPlus since it only deals with .XSLX file types, not my required .XLS.

在c#中,如何创建一个密码保护(用户必须输入密码才能打开文件).XLS文件而无需安装Excel(因此不需要互操作)?NPOI和ExcelLibrary看起来很有希望(因为它们是免费的!),但无论它们是否支持密码保护,我似乎都找不到。我不能使用EPPlus,因为它只处理. xslx文件类型,而不是我需要的。xls。

Also, I would like to use an array to populate the data, not cell by cell. Here's what I did to accomplish this when I used Interop in the past, which was infinitely faster than cell by cell method:

另外,我希望使用数组来填充数据,而不是逐个单元地填充数据。这是我在过去使用Interop时完成的,它比cell方法快得多:

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;

2 个解决方案

#1


0  

Export code

出口代码

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

Password part im working on.

我正在处理密码部分。

#2


0  

Check the souce code sample from: http://forums.asp.net/t/1831409.aspx

查看souce代码示例:http://forums.asp.net/t/1831409.aspx

You can also populate the data by a dataset, not cell by cell.

您还可以通过数据集填充数据,而不是单元格填充数据。

#1


0  

Export code

出口代码

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

Password part im working on.

我正在处理密码部分。

#2


0  

Check the souce code sample from: http://forums.asp.net/t/1831409.aspx

查看souce代码示例:http://forums.asp.net/t/1831409.aspx

You can also populate the data by a dataset, not cell by cell.

您还可以通过数据集填充数据,而不是单元格填充数据。