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

3
在C#中,我该如何创建一个PASSWORD PROTECTED(用户必须输入密码才能完全打开文件).XLS文件而不需要安装Excel(因此不需要Interop)? 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 对于一个有趣的问题...我本以为答案是否定的,但我很想看看你是否会得到任何回复。 - Sheridan
2个回答

0

导出代码

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

我正在处理密码部分。


当你弄清楚密码部分时,请告诉我。 - Theodosius Von Richthofen

0

感谢回答。看起来EasyXLS是试用版,所以您需要购买该产品。 - Theodosius Von Richthofen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接