JAVA excel 水印生成 代码
时间: 2023-09-10 10:04:47 浏览: 56
### 回答1:
Here is an example of how you can add a watermark to an Excel file in Java using Apache POI:
```
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class WatermarkExcel {
public static void main(String[] args) throws Exception {
String inputFile = "input.xlsx";
String outputFile = "output.xlsx";
FileInputStream in = new FileInputStream(inputFile);
Workbook workbook = WorkbookFactory.create(in);
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("CONFIDENTIAL");
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setColor(IndexedColors.GREY_50_PERCENT.getIndex());
font.setFontHeightInPoints((short) 48);
font.setFontName("Arial");
style.setFont(font);
style.setWrapText(true);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
sheet.setDefaultColumnWidth(256 * 15);
sheet.setDefaultRowHeightInPoints(256 * 15);
for (int i = 0; i < sheet.getLastRowNum() + 1; i++) {
row = sheet.getRow(i);
if (row != null) {
for (int j = 0; j < row.getLastCellNum(); j++) {
cell = row.getCell(j);
if (cell != null) {
cell.setCellStyle(style);
}
}
}
}
FileOutputStream out = new FileOutputStream(outputFile);
workbook.write(out);
out.close();
workbook.close();
in.close();
}
}
```
This code creates a watermark "CONFIDENTIAL" in the first row of the first sheet of the input Excel file. It uses Apache POI library to read the input Excel file, create the watermark and write the output Excel file. The watermark is created with a gray color, 48-point Arial font, centered both horizontally and vertically.
### 回答2:
在Java中生成Excel水印,我们可以使用Apache POI库来实现。下面是一个简单的示例代码:
```java
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelWatermarkGenerator {
public static void main(String[] args) {
// 创建一个工作簿
Workbook workbook = new HSSFWorkbook();
// 创建一个工作表
Sheet sheet = workbook.createSheet("Sheet1");
// 添加水印
Drawing drawing = sheet.createDrawingPatriarch();
CreationHelper helper = workbook.getCreationHelper();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(0); // 设置水印起始列
anchor.setRow1(0); // 设置水印起始行
Drawing waterDrawing = drawing.createGroup(anchor);
// 创建文本框并设置水印内容
TextBox textBox = waterDrawing.createTextbox(new CellAddress(0, 0),
new CellAddress(0, 0));
textBox.setText("水印内容");
// 保存工作簿到文件
try (FileOutputStream fileOutputStream = new FileOutputStream("output.xls")) {
workbook.write(fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
以上代码使用Apache POI库创建了一个Excel工作簿,并在工作表上生成了一个水印。使用Drawing对象创建一个Group,并在其中创建一个Textbox用于显示水印内容。最后将工作簿保存到文件中。
请注意,你需要将Apache POI库添加到你的项目依赖中,以便能够成功编译和运行以上代码。
希望这个简单的示例能够帮助你生成Excel水印。如果你需要更复杂的水印效果,你可以进一步研究和尝试更多Apache POI提供的功能和API。
### 回答3:
JAVA excel 水印生成代码可以使用Apache POI库来操作Excel文件。下面是一个示例代码:
```
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelWatermarkGenerator {
public static void main(String[] args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
Drawing drawing = sheet.createDrawingPatriarch();
CreationHelper helper = workbook.getCreationHelper();
//创建水印文字
ClientAnchor anchor = helper.createClientAnchor();
anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
anchor.setCol1(0);
anchor.setRow1(0);
anchor.setCol2(7);
anchor.setRow2(7);
Comment comment = drawing.createCellComment(anchor);
RichTextString str = helper.createRichTextString("水印文字");
comment.setString(str);
comment.setVisible(false);
Cell cell = sheet.createRow(0).createCell(0);
cell.setCellComment(comment);
try {
FileOutputStream fileOut = new FileOutputStream("watermark.xlsx");
workbook.write(fileOut);
fileOut.close();
System.out.println("水印生成成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
该代码使用Apache POI库创建了一个XSSFWorkbook对象,然后创建了一个Sheet对象并在该表格上创建一个Drawing对象,用于绘制水印。
利用CreationHelper对象创建ClientAnchor对象并设置水印文字所占范围,然后创建CellComment对象并设置水印文字内容,最后将CellComment对象设置为指定单元格的注释。
最后将生成的Excel文件写入文件系统中。
以上是一个简单的JAVA excel水印生成代码示例。代码可以根据实际需求进行调整和扩展。