1

我正在使用以下 POI api 通过 java 代码在 excel 上编写

public static HSSFWorkbook sampleWorkbook = new HSSFWorkbook();
public static HSSFSheet sampleDataSheet = sampleWorkbook.createSheet("ABC");

它在 excel 中创建一个名为 ABC 的工作表。没关系我的问题是我需要在同一个工作簿中添加/创建另一个名为“XYZ”的工作表。我怎么做?好像我写了这样的东西

public static HSSFSheet sampleDataSheet = sampleWorkbook.createSheet("XYZ");

它将覆盖第一个(ABC)..

4

3 回答 3

3

不会覆盖第一个,它将创建一个新工作表,但您需要将其分配给不同的变量:

HSSFSheet firstSheet = sampleWorkbook.createSheet("ABC");
HSSFSheet secondSheet = sampleWorkbook.createSheet("XYZ");

这是文档

公共 XSSFSheet createSheet(java.lang.String sheetname)

为此工作簿创建一个新工作表并返回高级表示。使用它来创建新工作表。

于 2012-08-19T12:10:25.270 回答
2

澄清一下,您可以将相同的变量名称分配给 2 个不同的工作表,这 2 个工作表将被创建。但是,这不是一个好的做法,因为您失去了创建的第一个工作表的句柄。

编码:

public static HSSFSheet sampleDataSheet = sampleWorkbook.createSheet("ABC");

建议您在班级成员部分创建一个新工作表。不建议这样做,因为诸如sampleWorkbook可能尚未初始化的因变量。

用于工作表/Excel 文件创建的方法可能如下所示:

private void createExcelFile() {
   HSSFWorkbook workbook = new HSSFWorkbook();
   HSSFSheet sampleDataSheet1 = workbook.createSheet("ABC");
   HSSFSheet sampleDataSheet2 = workbook.createSheet("XYZ");
   // save to disk
}

有关更多信息,请参阅:

公共 HSSFSheet createSheet()

于 2012-08-19T12:47:21.547 回答
0

用excel写

public class Writesheet 
{

 public static void main(String[] args) throws Exception 

 {

  //Create blank workbook
  XSSFWorkbook workbook = new XSSFWorkbook(); 
  //Create a blank sheet
  XSSFSheet spreadsheet = workbook.createSheet( 
  " Employee Info ");
  XSSFSheet spreadsheet1 = workbook.createSheet( 
          " Employee Info1 ");
  XSSFSheet spreadsheet2 = workbook.createSheet( 
          " Employee Info2 ");
  XSSFSheet spreadsheet3 = workbook.createSheet( 
          " Employee Info3 ");
  XSSFSheet spreadsheet4 = workbook.createSheet( 
          " Employee Info4 ");
  XSSFSheet spreadsheet5 = workbook.createSheet( 
          " Employee Info5 ");


  XSSFSheet spreadsheet9 = workbook.createSheet( 

  //Create row object
//This data needs to be written (Object[])
  Map < String, Object[] > empinfo =  new TreeMap < String, Object[] >();
  empinfo.put( "1", new Object[] { "EMP ID", "EMP NAME", "DESIGNATION" });
  empinfo.put( "2", new Object[] { "tp1", "Gopal", "Technical Manager" });
  empinfo.put( "3", new Object[] {  "tp2", "Manisha", "Proof Reader" });
  empinfo.put( "4", new Object[] { "tp3", "Masthan", "Technical Writer" });
  empinfo.put( "5", new Object[] {  "tp4", "Satish", "Technical Writer" });
  empinfo.put( "6", new Object[] { "tp5", "Krishna", "Technical Writer" });

  Map < String, Object[] > empinfo1 =  new TreeMap < String, Object[] >();
  empinfo1.put( "1", new Object[] { "tp6", "Gop", "Technical Manager" });
  empinfo1.put( "2", new Object[] {  "tp7", "Mani", "Proof Reader" });
  empinfo1.put( "3", new Object[] { "tp8", "than", "lyricist" });
  empinfo1.put( "4", new Object[] {  "tp9", "Sat", " Writer" });
  empinfo1.put( "5", new Object[] { "tp10", "Krish", "Technical Writer" });

  Map < String, Object[] > empinfo2 =  new TreeMap < String, Object[] >();
  empinfo2.put( "1", new Object[] { "tp11", "nayan", " Manager" });
  empinfo2.put( "2", new Object[] {  "tp12", "priyanka", " developer" });
    empinfo2.put( "3", new Object[] { "tp13", "pradyot", "Technical Writer" });
  empinfo2.put( "4", new Object[] {  "tp14", "manisa", "developer" });
  empinfo2.put( "5", new Object[] { "tp15", "limca", "Technical Writer" });

  //Iterate over data and write to sheet
 func1( spreadsheet,empinfo);

 func1( spreadsheet1,empinfo1);

 func1( spreadsheet2,empinfo2);


 func1( spreadsheet3,empinfo);

 func1( spreadsheet4,empinfo1);

 func1( spreadsheet5,empinfo2);

  //Write the workbook in file system
  FileOutputStream out = new FileOutputStream( 
  new File("Writesheet2.xlsx"));
  workbook.write(out);
  out.close();
  System.out.println( 
  "Writesheet.xlsx written successfully" );
  workbook.close();

  }




 static void func1(XSSFSheet var,Map<String, Object[]> var1)
   {


   XSSFRow row;
Set < String > keyid = var1.keySet();
   int rowid = 0;
   for (String key : keyid)
   {

    row = var.createRow(rowid++);
       Object [] objectArr = var1.get(key);
       int cellid = 0;
       for (Object obj : objectArr)
        {
           Cell cell = row.createCell(cellid++);
           cell.setCellValue((String)obj);
        }
   }
 }

 }
于 2016-05-03T09:38:34.490 回答