NPOI实现excel 2003 在线审批签字功能,缺陷不知如何进行工作簿保护?求解

 /// <summary>
///
/// </summary>
/// <param name="filePath"></param>
/// <param name="newFilePath"></param>
/// <returns></returns>
public string CreateSignExcel(string filePath, string newFilePath, DataTable dt)
{
if (File.Exists(newFilePath))
File.Delete(newFilePath);
FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
HSSFWorkbook hssfworkbook;
try
{
hssfworkbook = new HSSFWorkbook(file);
}
catch (NPOI.EncryptedDocumentException exe)//工作簿写保护报错
{
return "";
}
int FileSheet = 0;
FileSheet = hssfworkbook.NumberOfSheets;
//npoi获取列数
int firstrownum = 0, maxRowNum = 0, maxColNum = 0;
ICellStyle cellStyle = hssfworkbook.CreateCellStyle();
cellStyle.WrapText = true;
//纵
cellStyle.VerticalAlignment = VerticalAlignment.CENTER;
//横
cellStyle.Alignment = HorizontalAlignment.CENTER;
try
{
for (int i = 0; i < FileSheet; i++)
{
HSSFSheet sheet = (HSSFSheet)hssfworkbook.GetSheetAt(i);
HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
int hidecolemns = 0;//判断最大隐藏行
sheet.IsActive = true;
maxRowNum = sheet.LastRowNum; //最大行数
firstrownum = sheet.FirstRowNum;//开始行数
System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
while (rows.MoveNext())
{
IRow row = (HSSFRow)rows.Current;
if (maxColNum < row.LastCellNum)
maxColNum = row.LastCellNum;
}
if (maxRowNum < 2)
{
//锁定单元格
//sheet.Protect = true;//如何动态去到密码保护,另外如何设置密码
sheet.ProtectSheet("1");
continue;
}
maxRowNum += 1;
int a = 0;
#region 签字
for (int k = 0; k < dt.Rows.Count; k++)
{
if (a == 0)
{
for (int l = 0; l < 4; l++)
{
IRow row1;
row1 = sheet.GetRow(maxRowNum + 1 + l);
if (row1 == null)
{
row1 = sheet.CreateRow(maxRowNum + 1 + l);
}
for (int m = 0; m < 3; m++)
{
ICell cell1;
cell1 = row1.GetCell(m);
if (cell1 == null)
{
cell1 = row1.CreateCell(m);
cell1.CellStyle = cellStyle;
}
}
row1.GetCell(0).SetCellValue(dt.Rows[k]["UserDscp"].ToString());
}
CellRangeAddress region = new CellRangeAddress(maxRowNum + 1, maxRowNum + 4, 0, 0);
sheet.AddMergedRegion(region);
}
else
{
for (int l = 0; l < 4; l++)
{
IRow row1;
row1 = sheet.GetRow(maxRowNum + 1 + l);
if (row1 == null)
{
row1 = sheet.CreateRow(maxRowNum + 1 + l);
}
for (int m = 3; m < 6; m++)
{
ICell cell1;
cell1 = row1.GetCell(m);
if (cell1 == null)
{
cell1 = row1.CreateCell(m);
cell1.CellStyle = cellStyle;
}
}
row1.GetCell(3).SetCellValue(dt.Rows[k]["UserDscp"].ToString());
}
CellRangeAddress region = new CellRangeAddress(maxRowNum + 1, maxRowNum + 4, 3, 3);
sheet.AddMergedRegion(region);
}
string appresulte = dt.Rows[k]["AppResult"].ToString();
if (appresulte == string.Empty || appresulte == "NULL")//此时未审核
{
IRow row1 = sheet.GetRow(maxRowNum + 1);
IRow row2 = sheet.GetRow(maxRowNum + 2);
IRow row3 = sheet.GetRow(maxRowNum + 3);
IRow row4 = sheet.GetRow(maxRowNum + 4);
if (a == 0)
{
row1.GetCell(1).SetCellValue("未审核");
row1.GetCell(2).SetCellValue("未审核");
row2.GetCell(1).SetCellValue("未审核");
row2.GetCell(2).SetCellValue("未审核");
row3.GetCell(1).SetCellValue("未审核");
row3.GetCell(2).SetCellValue("未审核");
row4.GetCell(1).SetCellValue("未审核");
row4.GetCell(2).SetCellValue("未审核");
}
else
{
row1.GetCell(4).SetCellValue("未审核");
row1.GetCell(5).SetCellValue("未审核");
row2.GetCell(4).SetCellValue("未审核");
row2.GetCell(5).SetCellValue("未审核");
row3.GetCell(4).SetCellValue("未审核");
row3.GetCell(5).SetCellValue("未审核");
row4.GetCell(4).SetCellValue("未审核");
row4.GetCell(5).SetCellValue("未审核");
}
}
else
{
if (appresulte == "2")//拒绝
{
string apprue = dt.Rows[k]["approvername"].ToString() + "拒绝(" + dt.Rows[k]["appcause"].ToString() + ")";
IRow row1 = sheet.GetRow(maxRowNum + 1);
IRow row2 = sheet.GetRow(maxRowNum + 2);
IRow row3 = sheet.GetRow(maxRowNum + 3);
IRow row4 = sheet.GetRow(maxRowNum + 4);
if (a == 0)
{
row1.GetCell(1).SetCellValue(apprue);
row1.GetCell(2).SetCellValue(apprue);
row2.GetCell(1).SetCellValue(apprue);
row2.GetCell(2).SetCellValue(apprue);
row3.GetCell(1).SetCellValue(apprue);
row3.GetCell(2).SetCellValue(apprue);
row4.GetCell(1).SetCellValue(apprue);
row4.GetCell(2).SetCellValue(apprue);
}
else
{
row1.GetCell(4).SetCellValue(apprue);
row1.GetCell(5).SetCellValue(apprue);
row2.GetCell(4).SetCellValue(apprue);
row2.GetCell(5).SetCellValue(apprue);
row3.GetCell(4).SetCellValue(apprue);
row3.GetCell(5).SetCellValue(apprue);
row4.GetCell(4).SetCellValue(apprue);
row4.GetCell(5).SetCellValue(apprue);
}
}
else
{
int startcol = 1;// endcol = 2;
if (a == 1)
{
startcol = 4;
//endcol = 4;
}
if (dt.Rows[k]["qz"].ToString().Trim() != "" && dt.Rows[k]["qz"].ToString().Trim() != "NULL")//签字同意且存在用户图片
{
byte[] bytes;
bytes = (byte[])dt.Rows[k]["qz"];

if (a == 0)
{
int pictureIdx = hssfworkbook.AddPicture(bytes, PictureType.JPEG);
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, startcol, maxRowNum + 1, 3, maxRowNum + 4);//后四位 第几列开始第几行开始,共几列到第几行
HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
}
else
{
int pictureIdx1 = hssfworkbook.AddPicture(bytes, PictureType.JPEG);
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 0, 0, 4, maxRowNum + 1, 6, maxRowNum + 4);//后四位 第几列开始第几行开始,共几列到第几行
HSSFPicture pict1 = (HSSFPicture)patriarch.CreatePicture(anchor1, pictureIdx1);
}
}
else
{
string apprue = dt.Rows[k]["approvername"].ToString() + "同意(" + dt.Rows[k]["appcause"].ToString() + ")";
IRow row1 = sheet.GetRow(maxRowNum + 1);
IRow row2 = sheet.GetRow(maxRowNum + 2);
IRow row3 = sheet.GetRow(maxRowNum + 3);
IRow row4 = sheet.GetRow(maxRowNum + 4);
if (a == 0)
{
row1.GetCell(1).SetCellValue(apprue);
row1.GetCell(2).SetCellValue(apprue);
row2.GetCell(1).SetCellValue(apprue);
row2.GetCell(2).SetCellValue(apprue);
row3.GetCell(1).SetCellValue(apprue);
row3.GetCell(2).SetCellValue(apprue);
row4.GetCell(1).SetCellValue(apprue);
row4.GetCell(2).SetCellValue(apprue);
}
else
{
row1.GetCell(4).SetCellValue(apprue);
row1.GetCell(5).SetCellValue(apprue);
row2.GetCell(4).SetCellValue(apprue);
row2.GetCell(5).SetCellValue(apprue);
row3.GetCell(4).SetCellValue(apprue);
row3.GetCell(5).SetCellValue(apprue);
row4.GetCell(4).SetCellValue(apprue);
row4.GetCell(5).SetCellValue(apprue);
}
}
}
}
if (a == 0)
{
a = 1;
}
else
{
a = 0;
maxRowNum += 4;
}
}
#endregion
sheet.ProtectSheet("1");
}
}
catch (Exception exp)
{
hssfworkbook.Dispose();
file.Close();
file.Dispose();
return "";
}

    //这里遗憾的是缺少保护工作簿(保护结构和窗口),实现excel在线审批签字功能,如果不能进行保护工作簿,导致用户可以新建工作表删除原有工作表。

FileStream file2 = new FileStream(newFilePath, FileMode.Create);

hssfworkbook.Write(file2);
file.Close();
file.Dispose();
file2.Close();
file2.Dispose();
hssfworkbook.Dispose();
return newFilePath;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值