2012-06-21 90 views
11

Tôi đang tạo tệp excel (.xlsx) bằng cách sử dụng poi (java). Sau khi tôi tạo tệp excel, tôi thấy tệp excel Tác giả là "Apache POI". Có cách nào để thay đổi điều đó không?Cách đặt tên Tác giả thành tệp excel bằng cách sử dụng poi

Đây là mã tôi đang sử dụng để tạo ra file excel ...

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; 

    } 
} 

Trả lời

15

Nó khá đơn giản:

HSSF:

SummaryInformation summaryInfo = workbook.getSummaryInformation(); 
summaryInfo.setAuthor(author); 

XSSF:

POIXMLProperties xmlProps = workbook.getProperties();  
POIXMLProperties.CoreProperties coreProps = xmlProps.getCoreProperties(); 
coreProps.setCreator(author); 

Have fun :)

+0

Bạn có thể vui lòng cho tôi biết xmlProps này dành cho XSSF không? – user1430989

+0

Cảm ơn bạn .. Tôi hiểu rồi. – user1430989

+0

Xin chào Olivier Coilland, trong trường hợp XSSF hoạt động tốt. Nhưng trong trường hợp HSSF, SummaryInformation summaryInfo = workbook.getSummaryInformation(); summaryInfo là null .. và không hoạt động .. – user1430989

5

Không phải là một câu trả lời trực tiếp nhưng trong trường hợp bất cứ ai cần phải làm điều này trong NPOI (cảng NET của POI) đây là phương pháp mở rộng tôi đã đưa ra ... đó là cơ bản những gì Olivier được đề xuất - chỉ được triển khai trong C#:

/// <summary> 
/// Sets the author of this workbook. 
/// </summary> 
/// <param name="workbook"></param> 
/// <param name="author"></param> 
public static void SetAuthor(this IWorkbook workbook, string author) 
{ 
    if (workbook is NPOI.XSSF.UserModel.XSSFWorkbook) 
    { 
     var xssfWorkbook = workbook as NPOI.XSSF.UserModel.XSSFWorkbook; 
     var xmlProps = xssfWorkbook.GetProperties(); 
     var coreProps = xmlProps.CoreProperties; 
     coreProps.Creator = author; 
     return; 
    } 

    if (workbook is NPOI.HSSF.UserModel.HSSFWorkbook) 
    { 
     var hssfWorkbook = workbook as NPOI.HSSF.UserModel.HSSFWorkbook; 
     var summaryInfo = hssfWorkbook.SummaryInformation; 

     if (summaryInfo != null) 
     { 
      summaryInfo.Author = author; 
      return; 
     } 

     var newDocInfo = NPOI.HPSF.PropertySetFactory.CreateDocumentSummaryInformation(); 

     var newInfo = NPOI.HPSF.PropertySetFactory.CreateSummaryInformation(); 
     newInfo.Author = author; 

     hssfWorkbook.DocumentSummaryInformation = newDocInfo; 
     hssfWorkbook.SummaryInformation = newInfo; 

     return; 
    } 
} 
Các vấn đề liên quan