本文整理汇总了C#中NPOI.HSSF.UserModel.HSSFWorkbook.WriteProtectWorkbook方法的典型用法代码示例。如果您正苦于以下问题:C# HSSFWorkbook.WriteProtectWorkbook方法的具体用法?C# HSSFWorkbook.WriteProtectWorkbook怎么用?C# HSSFWorkbook.WriteProtectWorkbook使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NPOI.HSSF.UserModel.HSSFWorkbook
的用法示例。
在下文中一共展示了HSSFWorkbook.WriteProtectWorkbook方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestWriteProtection
public void TestWriteProtection()
{
HSSFWorkbook wb = new HSSFWorkbook();
Assert.IsFalse(wb.IsWriteProtected);
wb.WriteProtectWorkbook("mypassword", "myuser");
Assert.IsTrue(wb.IsWriteProtected);
wb.UnwriteProtectWorkbook();
Assert.IsFalse(wb.IsWriteProtected);
}
示例2: Export
/// <summary>
/// DataTable导出到Excel的MemoryStream 第二步
/// </summary>
/// <param name="dtSource">源DataTable</param>
/// <param name="strHeaderText">表头文本</param>
public static MemoryStream Export(DataTable dtSource, string strHeaderText, bool usePassword, string path)
{
SystemFactory systemFactory = new SystemFactory();
ISystemService systemService = systemFactory.GetSystemService();
SystemParamVO systemParamVO = systemService.GetSystemParamByRoot();
if (string.IsNullOrEmpty(path))
{
path = @"D:\temp.xls";
}
if (File.Exists(path))
{
File.Delete(path);
}
HSSFWorkbook fakeWorkbook = new HSSFWorkbook();
HSSFSheet fakeSheet = fakeWorkbook.CreateSheet() as HSSFSheet;
FileStream savefile = new FileStream(path, FileMode.Create);
fakeWorkbook.Write(savefile);
savefile.Close();
savefile.Dispose();
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
if (usePassword && systemParamVO!=null && !string.IsNullOrEmpty(systemParamVO.FilePassword))
{
NPOI.HSSF.Record.Crypto.Biff8EncryptionKey.CurrentUserPassword = "12345";//打开前调用
}
HSSFWorkbook workbook = new HSSFWorkbook(file);
if (usePassword && systemParamVO != null && !string.IsNullOrEmpty(systemParamVO.FilePassword))
{
workbook.WriteProtectWorkbook(systemParamVO.FilePassword, "");//设置新密码
}
file.Close();
HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0);
#region 右击文件 属性信息
{
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "NPOI";
workbook.DocumentSummaryInformation = dsi;
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Author = "文件作者信息"; //填加xls文件作者信息
si.ApplicationName = "创建程序信息"; //填加xls文件创建程序信息
si.LastAuthor = "最后保存者信息"; //填加xls文件最后保存者信息
si.Comments = "作者信息"; //填加xls文件作者信息
si.Title = "标题信息"; //填加xls文件标题信息
si.Subject = "主题信息";//填加文件主题信息
si.CreateDateTime = DateTime.Now;
workbook.SummaryInformation = si;
}
#endregion
HSSFCellStyle dateStyle = workbook.CreateCellStyle() as HSSFCellStyle;
HSSFDataFormat format = workbook.CreateDataFormat() as HSSFDataFormat;
dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");
//取得列宽
int[] arrColWidth = new int[dtSource.Columns.Count];
foreach (DataColumn item in dtSource.Columns)
{
arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length;
}
for (int i = 0; i < dtSource.Rows.Count; i++)
{
for (int j = 0; j < dtSource.Columns.Count; j++)
{
int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][j].ToString()).Length;
if (intTemp > arrColWidth[j])
{
arrColWidth[j] = intTemp;
}
}
}
int rowIndex = 0;
foreach (DataRow row in dtSource.Rows)
{
#region 新建表,填充表头,填充列头,样式
if (rowIndex == 65535 || rowIndex == 0)
{
if (rowIndex != 0)
{
sheet = workbook.CreateSheet() as HSSFSheet;
}
#region 表头及样式
{
if (string.IsNullOrEmpty(strHeaderText))
{
HSSFRow headerRow = sheet.CreateRow(0) as HSSFRow;
headerRow.HeightInPoints = 25;
headerRow.CreateCell(0).SetCellValue(strHeaderText);
//.........这里部分代码省略.........