title: How to convert a xlsx file to a text file so that text file to get the exact structure in the text file and how to manage the mergerd cells
body: <p>I am working with a xlsx file which  have structure and i have to exactly copy the structure to the text file.My code is  copying all the data from the xlsx file and pasting them into the txt file, but the structure is not right at all.I am giving the complete code as well as the xlsx file </p>
  
  <p><a href="https://i.stack.imgur.com/ebGg6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ebGg6.png" alt="enter image description here"></a></p>
  
  <p>And the code i am using is </p>
  
  <pre><code>public class Test {
  
  public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
      PrintWriter writer = new PrintWriter("D:\\data.txt", "UTF-8");
      try {
  
          FileInputStream file = new FileInputStream(new File("D:\\resources\\BLF_20170111_BDPM_C.xlsx"));
  
          //Get the workbook instance for XLS file 
          XSSFWorkbook workbook = new XSSFWorkbook (file);
  
          //Get first sheet from the workbook
          XSSFSheet sheet = workbook.getSheetAt(0);
  
  
          //Iterate through each rows from first sheet
          Iterator&lt;Row&gt; rowIterator = sheet.iterator();
          while(rowIterator.hasNext()) {
              Row row = rowIterator.next();
  
              //For each row, iterate through each columns
              Iterator&lt;Cell&gt; cellIterator = row.cellIterator();
              while(cellIterator.hasNext()) {
  
                  Cell cell = cellIterator.next();
  
                  switch(cell.getCellType()) {
                      case Cell.CELL_TYPE_BOOLEAN:
                          System.out.print(cell.getBooleanCellValue() + "\t\t");
                          writer.print(cell.getBooleanCellValue() + "\t\t");
                          break;
                      case Cell.CELL_TYPE_NUMERIC:
                          System.out.println(cell.getNumericCellValue() + "\t\t");
                          writer.println(cell.getNumericCellValue() + "\t\t");
                          break;
                      case Cell.CELL_TYPE_STRING:
                          System.out.println(cell.getStringCellValue() + "\t\t"); // here i am writing the data to the text file.
                          writer.println(cell.getStringCellValue()+ "\t\t");
                          break;
                  }
              }
              System.out.println("");
          }
          file.close();
          FileOutputStream out = 
              new FileOutputStream(new File("D:\\writedatasamp.txt"));
          workbook.write(out);
          out.close();
  
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }finally {
          writer.close();
      }
  
  }
  
  }
  </code></pre>
  
  <p>But the problem is the data is coming </p>
  
  <p>Sponsorship<br>
  Part of a Merged Cell<br>
  Part of a Merged Cell<br>
  Part of a Merged Cell<br>
  Part of a Merged Cell<br>
  Part of a Merged Cell<br>
  Part of a Merged Cell<br>
  Part of a Merged Cell<br>
  Part of a Merged Cell<br>
  Part of a Merged Cell<br>
  Part of a Merged Cell<br>
  HJL<br>
  Post Priviledge<br>
  Counting starts from 01-01-2018 to 30-11-2018<br>
  Starting  date 16-Jan-2019<br>
  Name1<br>
  Name2<br>
  Name3<br>
  Name4<br>
  Name5<br>
  Name6<br>
  Part of a Merged Cell<br>
  Name7<br>
  Part of a Merged Cell<br>
  Name8<br>
  Name9<br>
  loc. ccy<br>
  admin. ccy<br>
  loc. ccy<br>
  admin. ccy<br>
  admin. ccy<br>
  admin. ccy<br>
  India (1.2%)<br>
  ARC 56<br>
  INR<br>
  0.0<br>
  0.0<br>
  0.0<br>
  0.0<br>
  0.0<br>
  0.0<br>
  0.0<br>
  0.0<br>
  ARC 57<br>
  INR<br>
  0.0<br>
  0.0<br>
  0.0<br>
  0.0<br>
  0.0<br>
  0.0<br>
  0.0<br>
  0.0<br>
  ARC 58<br>
  INR<br>
  0.0<br>
  0.0<br>
  0.0<br>
  0.0<br>
  0.0<br>
  0.0<br>
  0.0     </p>
  
  <p>So can i get the same structute as the xlsx file ? and more over in the sponsorship row other cells are merged so its giving a message i.e  Part of a Merged Cell. How i can solve this problem?</p>