简体   繁体   English

从java导出数据到excel

[英]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. 只有6000多行。 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. 我想要做的是获取其rt值在2的范围内的每个mz值之间的差异。例如,使用此帖子上的图像,第2行中的mz值(100.9035)和行中的mz值6(102.9007)的RT值分别为8.07和7.36,即RT值彼此相差2。 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. 由此,我想从第二个mz值中减去第一个mz值的值来计算差值,然后将此信息导出到excel中。 I want this process to be done for all mz values that are within that 2 second range (either + or - 2). 我希望对于2秒范围内的所有mz值(+或 - 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). 为此,我首先将mz和rt值分离为两个单独的文本文件,然后创建一个2d双数组并从这两个文件中填充数组(这可能是一种不必要的方式,但我的脚本编写水平相当高最小)。 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. 在这样做之后,我然后使用if和for条件迭代数组,以找到彼此相差2 rt的mz值并获取它们之间的差异。 This is the script that I've written in java so far:- 这是我到目前为止用java编写的脚本: -

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. 我注意到在我尝试执行此操作之前,由于我编写的数据超出了电子表格的允许列范围,因此我不断提出有关列范围的错误,因此我尝试设计一种方法来继续编写一旦超出列范围(通过在代码中使用计数器实现的255列),数据到另一个工作表上,但我仍然收到错误消息。 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? 我知道java不是执行此任务的最理想的工具,但它是我有一点经验的唯一语言。有没有办法以我能克服允许的列范围问题的方式导出数据?

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. 我首先尝试将其保存为CSV文本文件,然后查看总共有多少列 - 您可能会破坏sheet2的限制,并且没有要移动到的Sheet3。

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 或者,或者,您可以拥有任意数量的行 - 将数据保存到行而不是列,将它们分成255个块,然后https://support.office.com/en-in/article/Switch -transpose柱和-行- ed1215f5-59af-47e6-953b-0b513b094dc2

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

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