简体   繁体   English

如何使用java将一个excel单元格拆分为两个单元格

[英]How to split one excel cell into two cells using java

I have spreadsheet and I need to spit the content of one cell into two cells. 我有电子表格,我需要将一个单元格的内容吐出到两个单元格中。

The cell content like this. 细胞内容是这样的。 Name 19:00:00 姓名19:00:00

I need to split it into two cells as Name and 19:00:00 我需要将它分成两个单元格作为Name和19:00:00

I'm using apache poi to read excel file. 我正在使用apache poi来读取excel文件。 Are there any better library to read the excel file and work with it. 有没有更好的库来阅读excel文件并使用它。 I need to delete several columns from the datasheet. 我需要从数据表中删除几个列。

You could do something likewise, 你也可以这样做,

step 1 : read value which is in your case is : "Name 19:00:00" into one temporary String, 第1步:在您的情况下读取值为:“将名称19:00:00”转换为一个临时字符串,

step 2 : unmerge cell using apache poi's workSheet.removeMergedRegion(index - of the region to unmerge). 步骤2:使用apache poi的workSheet.removeMergedRegion(索引 - 取消合并的区域)取消合并单元格。 refer this. 参考这个。

step 3 : split string what you received into step-1, from space, so you get array of String which is most likely, ary[0] = Name , ary 1 = 19:00:00 第3步:从太空中将你收到的字符串拆分为第1步,所以你得到最可能的字符串数组,ary [0] =姓名,ary 1 = 19:00:00

step 4 : write ary[0] into unmerged first cell and likewise for another one as per you wish. 步骤4:将ary [0]写入未合并的第一个单元格,同样根据您的意愿将其写入另一个单元格。

import java.io.File;
import java.io.FileInputStream;

import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Readsheet 
{
   static XSSFRow row;
   public static void main(String[] args) throws Exception 
   {
      FileInputStream fis = new FileInputStream(
      new File("G:\\test.xlsx"));
      XSSFWorkbook workbook = new XSSFWorkbook(fis);
      XSSFSheet spreadsheet = workbook.getSheetAt(0);
      Iterator < Row > rowIterator = spreadsheet.iterator();
      while (rowIterator.hasNext()) 
      {
         row = (XSSFRow) rowIterator.next();
         Iterator < Cell > cellIterator = row.cellIterator();
         while ( cellIterator.hasNext()) 
         {
            Cell cell = cellIterator.next();
            switch (cell.getCellType()) {
               case Cell.CELL_TYPE_NUMERIC:
               System.out.print( 
               cell.getNumericCellValue() + " ; " );
               break;
               case Cell.CELL_TYPE_STRING:
               System.out.print(
               cell.getStringCellValue() + " ; " ); 
               break;
               default:
            }
         }
         System.out.println();
      }
      fis.close();
   }
}

This is the code I used to read excel file. 这是我用来读取excel文件的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM