簡體   English   中英

如何使用密碼保護創建Excel 2003(.XLS)文件,而不使用c#安裝Excel?

[英]How to create Excel 2003 (.XLS) file with password protection without Excel installed with c#?

在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;

導出代碼

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

密碼部分我正在努力。

檢查來自以下網址的源代碼示例: http//forums.asp.net/t/1831409.aspx

您還可以按數據集填充數據,而不是逐個單元填充數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM