如何设定作者名称中使用POI到excel文件 [英] How to set Author name to excel file using poi
本文介绍了如何设定作者名称中使用POI到excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建使用POI(JAVA)的Excel(.xlsx)格式文件。之后,我创建Excel文件,我看到Excel文件作者作为阿帕奇POI。有什么办法来改变这种状况?
下面是code,我使用的是创建Excel文件...
进口java.io.FileOutputStream中;进口org.apache.poi.ss.usermodel.Cell;
进口org.apache.poi.ss.usermodel.Row;
进口org.apache.poi.ss.usermodel.Sheet;
进口org.apache.poi.xssf.usermodel.XSSFWorkbook;公共类CreateExcelFile { 公共静态无效的主要(字串[] args){
/ ** Excel文件的名称,我们要创建** /
字符串文件名=C:\\\\ \\\\温度testPOIWrite.xlsx
writeDataToExcelFile(文件名);
} / **此方法将数据写入新的Excel文件** /
私有静态无效writeDataToExcelFile(字符串文件名){ 的String [] [] = excelData $ P $每年preDataToWriteToExcel(); XSSFWorkbook myWorkBook =新XSSFWorkbook();
表MySheet的工作= myWorkBook.createSheet();
行myRow = NULL;
细胞了myCell = NULL; 对(INT的rowNum = 0;的rowNum&下; excelData [0]。长度;的rowNum ++){
myRow = mySheet.createRow(的rowNum); 对(INT cellNum = 0; cellNum 4;; cellNum ++){
了myCell = myRow.createCell(cellNum);
myCell.setCellValue(excelData第[ROWNUM] [cellNum]);
}
} 尝试{
FileOutputStream中出=新的FileOutputStream(文件名);
myWorkBook.write(出);
了out.flush();
out.close();
}赶上(例外五){
e.printStackTrace();
} } / ** prepare一些演示数据为Excel文件内容** /
公共静态的String [] [] $ P $每年preDataToWriteToExcel(){
的String [] [] = excelData新的String [4] [4];
excelData [0] [0] =名;
excelData [0] [1] =姓氏;
excelData [0] [2] =电话;
excelData [0] [3] =地址; excelData [1] [0] =Kushal;
excelData [1] [1] =Paudyal;
excelData [1] [2] =000-000-0000;
excelData [1] [3] =IL,USA; excelData [2] [0] =兰迪;
excelData [2] [1] =拉姆鲁宾逊;
excelData [2] [2] =111-111-1111;
excelData [2] [3] =TX,USA; excelData [3] [0] =菲尔;
excelData [3] [1] =柯林斯;
excelData [3] [2] =222-222-2222;
excelData [3] [3] =NY,USA; 返回excelData; }
}
解决方案
这是pretty简单:
HSSF:
的SummaryInformation SummaryInfo使用= workbook.getSummaryInformation();
summaryInfo.setAuthor(作者);
XSSF:
POIXMLProperties xmlProps = workbook.getProperties();
POIXMLProperties.CoreProperties coreProps = xmlProps.getCoreProperties();
coreProps.setCreator(作者);
玩得开心:)
I'm creating an excel (.xlsx) file using poi (java). After I create the excel file I see the excel file Author as "Apache POI". Is there any way to change that?
Here is the code I'm using to create excel file...
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateExcelFile {
public static void main(String[] args) {
/** Name of excel file that we are going to create **/
String fileName = "C:\\temp\\testPOIWrite.xlsx";
writeDataToExcelFile(fileName);
}
/** This method writes data to new excel file **/
private static void writeDataToExcelFile(String fileName) {
String[][] excelData = preapreDataToWriteToExcel();
XSSFWorkbook myWorkBook = new XSSFWorkbook();
Sheet mySheet = myWorkBook.createSheet();
Row myRow = null;
Cell myCell = null;
for (int rowNum = 0; rowNum < excelData[0].length; rowNum++) {
myRow = mySheet.createRow(rowNum);
for (int cellNum = 0; cellNum < 4; cellNum++) {
myCell = myRow.createCell(cellNum);
myCell.setCellValue(excelData[rowNum][cellNum]);
}
}
try {
FileOutputStream out = new FileOutputStream(fileName);
myWorkBook.write(out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/** Prepare some demo data as excel file content **/
public static String[][] preapreDataToWriteToExcel() {
String[][] excelData = new String[4][4];
excelData[0][0] = "First Name";
excelData[0][1] = "Last Name";
excelData[0][2] = "Telephone";
excelData[0][3] = "Address";
excelData[1][0] = "Kushal";
excelData[1][1] = "Paudyal";
excelData[1][2] = "000-000-0000";
excelData[1][3] = "IL,USA";
excelData[2][0] = "Randy";
excelData[2][1] = "Ram Robinson";
excelData[2][2] = "111-111-1111";
excelData[2][3] = "TX, USA";
excelData[3][0] = "Phil";
excelData[3][1] = "Collins";
excelData[3][2] = "222-222-2222";
excelData[3][3] = "NY, USA";
return excelData;
}
}
解决方案
It's pretty straightforward:
HSSF:
SummaryInformation summaryInfo = workbook.getSummaryInformation();
summaryInfo.setAuthor(author);
XSSF:
POIXMLProperties xmlProps = workbook.getProperties();
POIXMLProperties.CoreProperties coreProps = xmlProps.getCoreProperties();
coreProps.setCreator(author);
Have fun :)
这篇关于如何设定作者名称中使用POI到excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文