简体   繁体   中英

Exporting data to excel from java

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:-

在此输入图像描述 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, ie 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).

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:-

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 < 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 < 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 < 6427; j++) {

                Double rt2 = mzValues[j][1];

                Double mz2 = mzValues[j][0];

                Double rtDiff = rt2 - rt;

                int counter = 0;

                if (rtDiff < 2 && rtDiff > -2) {

                    counter++;

                    if(counter < 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();
        }

    }

}

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?

Thank you

I'd first try saving it as a CSV text file, then look to see how many columns you have total -- you might be busting the limit on sheet2, and don't have a sheet3 to move over to.

Or, alternately, you can have as many rows as you want -- save the data to rows instead of columns, break them up into chunks of 255, then https://support.office.com/en-in/article/Switch-transpose-columns-and-rows-ed1215f5-59af-47e6-953b-0b513b094dc2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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