title: Exporting data to excel from java
body: <p>I'll try my best to explain my problem so I apologise if parts of this post is confusing. I have two columns of data that looks like this:- </p>
  
  <p><img src="https://i.stack.imgur.com/k7yc9.jpg" alt="enter image description here"> There's just over 6000 rows. What I want to do is to take the difference between each mz value whose rt value is within a range of 2. So for example, using the image on this post, the mz value in row 2 (100.9035) and the mz value in row 6 (102.9007) both have RT values of 8.07 and 7.36 respectively, i.e. the RT values are within 2 from each other. From this, I want to subtract the value of the first mz value from the second to calculate the difference and to then export this information into excel. I want this process to be done for all mz values that are within that 2 second range (either + or - 2). </p>
  
  <p>To do this, I first separated out the mz and rt values into two separate text files and then created a 2d double array and populated the array from the two files (this might be an unnecessary way of doing this but my level of scripting is quite minimal). After doing so, I then iterate through the array using if and for conditions to find mz values that are within 2 rt from each other and to take the difference between them. This is the script that I've written in java so far:-</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.FileReader;
  import java.io.IOException;
  
  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  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.ss.usermodel.Workbook;
  
  public class qexactiveDiff {
  
      public static void main(String[] args) throws IOException {
  
          Workbook wb = new HSSFWorkbook();
          Sheet sheet1 = wb.createSheet("Data");
          Sheet sheet2 = wb.createSheet("AdditionalData");
  
  
          double[][] mzValues = new double[6427][2];
  
          BufferedReader mzBr = new BufferedReader(new FileReader(
                  "C:/Users/colles-a-l-kxc127/workspace/Maldi/src/Neg_mzs.txt"));
  
          BufferedReader rtBr = new BufferedReader(new FileReader(
                  "C:/Users/colles-a-l-kxc127/workspace/Maldi/src/Neg_rts.txt"));
  
          String mzLine = mzBr.readLine();
  
          String rtLine = rtBr.readLine();
  
          for (int i = 0; i &lt; 6427; i++) {
  
              if (mzValues != null || rtLine != null) {
  
                  mzValues[i][0] = Double.parseDouble(mzLine);
  
                  mzValues[i][1] = Double.parseDouble(rtLine);
  
                  mzLine = mzBr.readLine();
  
                  rtLine = rtBr.readLine();
  
              }
  
          }
  
          for (int i = 0; i &lt; 6427; i++) {     //6427 is num of rows    
  
  
              Double rt = mzValues[i][1];
              Double mz = mzValues[i][0];
  
              Row row = sheet1.createRow(i);
              Row row2 = sheet2.createRow(i);
              Cell cell = row.createCell(0);
              Cell cell2 = row2.createCell(0);
  
              cell.setCellValue(mz);
  
          //  System.out.println("Mz: " + mz + " | " + "RT: " + rt);
  
              for (int j = 0; j &lt; 6427; j++) {
  
                  Double rt2 = mzValues[j][1];
  
                  Double mz2 = mzValues[j][0];
  
                  Double rtDiff = rt2 - rt;
  
                  int counter = 0;
  
                  if (rtDiff &lt; 2 &amp;&amp; rtDiff &gt; -2) {
  
                      counter++;
  
                      if(counter &lt; 255){
  
                          cell = row.createCell(j + 1);
                          cell.setCellValue(mz2 - mz);
  
                      }
  
                      else{
  
                          cell2 = row.createCell(j + 1);
                          cell2.setCellValue(mz2 - mz);
  
                      }
  
              //      System.out.println("Index: " + j + " Mz difference: "
              //              + (mz2 - mz) + " RT: " + rt2);
  
                  }
              }
  
          }
  
          try {
  
              FileOutputStream output = new FileOutputStream("Data.xls");
              wb.write(output);
              output.close();
  
          } catch (Exception e) {
  
              e.printStackTrace();
          }
  
      }
  
  }
  </code></pre>
  
  <p>It might be a bit hard to follow as it's pretty dirty scripting, but I'm just trying to get the basics to work. I noticed when I tried to do this before I kept coming up with an error in regards to the column range as the data I was writing exceeded the allowable column range for the spreadsheet, so I tried to devise a method that would continue to write the data onto another sheet once it exceeded the column range (255 columns which I implemented through the use of a counter in the code), but I'm still getting the error message. I know java isn't the most ideal tool to perform this task but it's the only language that I have a bit of experience in. Is there a way to export the data in such a way that I can overcome the allowable column range issue?</p>
  
  <p>Thank you</p>