2014-01-10 74 views
4

在C#中,如何創建一個密碼保護(用戶必須輸入密碼才能打開該文件).XLS文件沒有安裝Excel(因此不是Interop)? NPOI和ExcelLibrary看起來很有前景(因爲它們是免費的!),但我無法找到任何地方,無論他們是否真的支持密碼保護。我不能使用EPPlus,因爲它只處理.XSLX文件類型,而不是我需要的.XLS。如何在不使用c#安裝Excel的情況下創建帶密碼保護的Excel 2003(.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; 
+0

對於一個有趣的問題+1我會認爲答案是否定的,但我很想看看你是否得到任何答覆。 – Sheridan

回答

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

密碼部分IM工作。

+0

讓我知道,當你找出密碼部分 –

0

檢查烴源代碼示例來自: http://forums.asp.net/t/1831409.aspx

您還可以通過數據集填充數據,而不是小區的小區。

+0

感謝您的答案。它確實顯示EasyXLS是一個試用版,所以你不得不購買該產品。 –

相關問題