JAVA excel 水印生成 代码
时间: 2023-09-21 18:05:55 浏览: 93
java水印实现代码
### 回答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可以使用Apache POI库来操作Excel文件,实现在Excel中生成水印。
首先,需要导入Apache POI库的相关jar包,并在代码中引入相关的类库。
下面是一个简单的示例代码,展示了如何在Excel文件中生成水印:
```
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
public class ExcelWatermarkGenerator {
public static void main(String[] args) throws Exception {
String excelFilePath = "path/to/your/excel/file.xlsx";
String watermarkImagePath = "path/to/your/watermark/image.png";
InputStream inputStream = new FileInputStream(watermarkImagePath);
BufferedImage watermarkImage = ImageIO.read(inputStream);
Workbook workbook = new HSSFWorkbook(new FileInputStream(excelFilePath));
HSSFSheet sheet = (HSSFSheet) workbook.getSheetAt(0);
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, 0, 0, 10, 10);
anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ImageIO.write(watermarkImage, "png", byteArrayOut);
byte[] bytes = byteArrayOut.toByteArray();
int pictureIndex = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
Picture picture = patriarch.createPicture(anchor, pictureIndex);
picture.resize();
FileOutputStream fileOut = new FileOutputStream(excelFilePath);
workbook.write(fileOut);
fileOut.close();
System.out.println("水印生成成功!");
}
}
```
以上代码首先加载要生成水印的Excel文件和水印图片,然后通过Apache POI操作Excel文件并创建一个画图器(HSSFPatriarch)对象。接下来,创建一个锚定器(HSSFClientAnchor)对象来定位水印的位置和大小。将水印图片写入字节数组输出流,并将该字节数组添加到Excel文件的picture集合中。最后,将修改后的Excel文件保存并关闭。
以上示例代码仅供参考,实际使用时可能需要根据具体需求进行修改。希望对你有所帮助!
### 回答3:
在Java中生成Excel水印的代码可以通过Apache POI库来实现。首先,我们需要导入Apache POI相关的包。
```java
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelWatermarkGenerator {
public static void main(String[] args) {
// 创建新的Excel工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
// 创建工作表
XSSFSheet sheet = workbook.createSheet("Sheet1");
try {
// 加载水印图片
File imageFile = new File("watermark.png");
byte[] byteArray = IOUtils.toByteArray(new FileInputStream(imageFile));
// 创建水印
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 0, 0, 3, 5);
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFPicture picture = drawing.createPicture(anchor, workbook.addPicture(byteArray, Workbook.PICTURE_TYPE_PNG));
picture.setAnchor(anchor);
// 保存Excel文件
FileOutputStream fileOut = new FileOutputStream("watermark_excel.xlsx");
workbook.write(fileOut);
fileOut.close();
System.out.println("水印生成成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
上述代码中,我们使用了XSSFWorkbook类来创建Excel工作簿,XSSFSheet类来创建工作表。通过文件输入流将水印图片加载到字节数组中,然后使用XSSFClientAnchor类和XSSFDrawing类来创建水印并将其添加到工作表中。最后,使用FileOutputStream将工作簿保存为Excel文件。
请确保你已经在项目中添加了Apache POI的相关依赖,以便代码能够正常运行。
阅读全文