在我的webform项目中,我在C#中使用Aspose
生成excel文件,并使用以下代码为其添加水印:
Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];
Aspose.Cells.Drawing.MsoPresetTextEffect effect = Aspose.Cells.Drawing.MsoPresetTextEffect.TextEffect2;
Aspose.Cells.Drawing.Shape wordart = sheet.Shapes.AddTextEffect(effect,"WATERMARKTEXT", "Arial Black", 47, false, false, 1, 1, 1, 1, 50, 300);
Aspose.Cells.Drawing.MsoFillFormat wordArtFormat = wordart.FillFormat;
wordArtFormat.ForeColor = System.Drawing.Color.Red;
wordArtFormat.Transparency = 0.8;
Aspose.Cells.Drawing.MsoLineFormat lineFormat = wordart.LineFormat;
lineFormat.IsVisible = false;
return workbook;
但我想在每张纸上水平和垂直重复这个水印。我怎么能这样做?
答案 0 :(得分:0)
使用Aspose.Cells API查看以下示例代码,以完成您的任务供您参考: 例如 示例代码:
Workbook workbook = new Workbook("e:\\test2\\Book1.xlsx");
//Add watermark to the first worksheet.
Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];
Aspose.Cells.Drawing.MsoPresetTextEffect effect = Aspose.Cells.Drawing.MsoPresetTextEffect.TextEffect2;
Aspose.Cells.Drawing.Shape wordart = sheet.Shapes.AddTextEffect(effect, "WATERMARKTEXT", "Arial Black", 47, false, false, 1, 1, 1, 1, 50, 300);
Aspose.Cells.Drawing.MsoFillFormat wordArtFormat = wordart.FillFormat;
wordArtFormat.ForeColor = System.Drawing.Color.Red;
wordArtFormat.Transparency = 0.8;
Aspose.Cells.Drawing.MsoLineFormat lineFormat = wordart.LineFormat;
lineFormat.IsVisible = false;
//Now add watermark to the second worksheet as it is.
Aspose.Cells.Worksheet worksheet1 = workbook.Worksheets[1];
worksheet1.Shapes.AddCopy(wordart, wordart.UpperLeftRow, wordart.Top, wordart.UpperLeftColumn, wordart.Left);
//Now add watermark to the third worksheet vertically.
Aspose.Cells.Worksheet worksheet2 = workbook.Worksheets[2];
worksheet2.Shapes.AddCopy(wordart, wordart.UpperLeftRow, wordart.Top, wordart.UpperLeftColumn, wordart.Left);
worksheet2.Shapes[0].RotationAngle = 90;
workbook.Save("e:\\test2\\out.xlsx");
注意:我在Aspose担任支持开发人员/传播者。
答案 1 :(得分:0)
我使用此方法设置背景而不是水印:
private static Aspose.Cells.Workbook AddExcelWaterMark(Aspose.Cells.Workbook workbook)
{
try
{
string path = HttpContext.Current.Server.MapPath("~/images/ExcelBackGround.png");
byte[] bgBuffer = File.ReadAllBytes(path);
foreach (Aspose.Cells.Worksheet sheet in workbook.Worksheets)
sheet.SetBackground(bgBuffer);
return workbook;
}
catch
{
return workbook;
}
}