简体   繁体   English

将字符串放在Excel中的另一个单元格中

[英]Put string in another cell in excel

public class Tab1 extends Fragment implements OnClickListener { 

    TextView text_fname, text_lname;
    EditText edit_fname, edit_lname;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {    

        View v = inflater.inflate(R.layout.tab1, container, false);

        text_fname = (TextView) v.findViewById(R.id.text1);
        text_lname = (TextView) v.findViewById(R.id.text2);

        edit_fname = (EditText) v.findViewById(R.id.edit1);
        edit_lname = (EditText) v.findViewById(R.id.edit2);

        Button button = (Button) v.findViewById(R.id.button1);
        button.setOnClickListener(this);

        return v;
    }

    @Override
    public void onClick(View arg0) {

        String fname1 = text_fname.getText().toString();
        String fname2 = edit_fname.getText().toString();

        String lname1 = text_lname.getText().toString();
        String lname2 = edit_lname.getText().toString();    

        String space = " ";
        String newLine = "\n";

        File file = null;
        FileOutputStream fos = null;

        try {
            file = getActivity().getFilesDir();
            fos = getActivity().openFileOutput("test.xls", Context.MODE_PRIVATE);
            fos.write(fname1.getBytes());
            fos.write(space.getBytes());
            fos.write(fname2.getBytes());
            fos.write(newLine.getBytes());
            fos.write(lname1.getBytes());
            fos.write(space.getBytes());
            fos.write(lname2.getBytes());
            fos.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (fos!=null) {
            try {
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        Toast.makeText(getActivity(), "File saved in " + file, Toast.LENGTH_LONG).show();
    }

}

How can I put the fname2 and lname2 in the next cell when I open it in excel? 在excel中打开fname2和lname2时,如何将其放在下一个单元格中? I tried adding " " but it only add space. 我尝试添加“”,但这只会增加空间。 Here's the current output: 这是当前的输出:

在此处输入图片说明

What I want is, the "mickey" and the "mouse" to be on the column B 我想要的是B列上的“ mickey”和“ mouse”

My understanding is that when you add a space it is literally just adding the character to your input, rather than telling Excel that you are looking at a new tab when you open it. 我的理解是,当您添加空格时,实际上只是在输入中添加了字符,而不是告诉Excel您在打开新选项卡时正在寻找它。

Try using \\s instead of a space. 尝试使用\\s代替空格。

It might also be possible that a tab, rather than a space, is interepreted as a new cell, so I would give \\t a go as well. 这也可能是可能是一个选项卡,而不是一个空间,是interepreted作为一种新的细胞,所以我会给\\t一个去为好。

Also worth looking at the .csv file format, rather than .xls as .csv uses comma separated values instead and may be easier to read/write as your application gets more complicated, as it can be read into a String and processed easier in Java. 同样值得一看的是.csv文件格式,而不是.xls因为.csv使用逗号分隔的值代替,并且随着应用程序变得更加复杂,它可能更易于读取/写入,因为可以将其读取为String并在Java中更轻松地处理。 。

excel see new column when ; excel何时见新栏; appears you should try space=";" 看来您应该尝试space=";"

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

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