如何创build带有密码保护的Excel 2003(.XLS)文件,而不使用c#安装Excel?

在C#中,我怎样才能创build一个密码保护(用户必须input密码打开文件的所有).XLS文件没有安装Excel(因此不互操作)? NPOI和ExcelLibrary看起来很有前途(因为它们是免费的!),但是我似乎无法find任何地方是否实际上支持密码保护。 我不能使用EPPlus,因为它只处理.XSLX文件types,而不是我需要的.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查看源代码示例

您也可以通过数据集来填充数据,而不是逐个单元格。