简体   繁体   English

将JTable导出为ex​​cel-转义下一列的值

[英]Exporting JTable to excel - escaping the value of the next column

In my file writer, I have these codes, the j == 4 and j == 8 is to execute a different code for the column that contains numbers 在我的文件编写器中,我有以下代码,j == 4和j == 8是为包含数字的列执行不同的代码

for(int i=0; i<model.getRowCount();i++){
        for(int j=0; j<model.getColumnCount();j++){
            if(j==4 || j==8){
                excelWriter.write("\""+model.getValueAt(i, j) + "\t"+"\""); 
            }
            else{
                excelWriter.write(model.getValueAt(i, j) + "\t");
            }
        }
        excelWriter.write("\n");
    }

the problem is after that column with number had been printed, the next column will not be printed.. for example 问题是在打印带有编号的列之后,将不打印下一列。

This is my JTable

| texta | textb | textc | textd | number | texte | textf | ....... 

in the excel file, it will be printed as....

| texta | textb | textc | textd | number | textf | ....... 

it skips the column beside the column with number... 它跳过带有数字的列旁边的列...

when i remove the 2nd "\\" in the code, the rest of the column concatenate to the column with number. 当我在代码中删除第二个“ \\”时,该列的其余部分将连接到具有数字的列。

Any idea how could I fix this? 知道我该如何解决吗? Thanks in advance 提前致谢

The second \\ have to be written before \\t I would guess. 第二个\\有之前写\\t我猜。

Wait... I have created an Excel sheet with text columns and number columns. 等等...我创建了一个带有文本列和数字列的Excel工作表。 Then I exported that sheet to a tab separated file. 然后,我将该工作表导出到制表符分隔的文件中。 When I open that file in a text editor the number isn't escaped on any kind. 当我在文本编辑器中打开该文件时,该数字不会以任何形式转义。 So I don't think you need this if . 所以我认为如果不需要的if

您可能的意思是:

excelWriter.write("\"" + model.getValueAt(i, j) + "\"\t");

for example, File can be with .xsl or .xlsx 例如,文件可以使用.xsl或.xlsx

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JTable;
import javax.swing.table.TableModel;

class ExcelExporterTodayFxDeals {

    ExcelExporterTodayFxDeals() {
    }

    public void exportTable(JTable table, File file) throws IOException {
        TableModel model = table.getModel();
        FileWriter out = new FileWriter(file);
        String diakritika = "áäÁÄčČďĎéěÉĚíÍľĺĽĹňŇóôöőÓÔÖŐŕřŔŘšŠťŤúüůÚÜŮýÝžŽ";
        String diakritikaBez = "aaAAcCdDeeEEiIllLLnNooooOOOOrrRRsStTuuuUUUyYzZ";
        String novyPreklad = "";
        String groupExport = "";
        Integer remDia = 1;
        for (int i = 0; i < (model.getColumnCount()); i++) {//* disable export from TableHeaders
            groupExport = String.valueOf(model.getColumnName(i));
            novyPreklad = groupExport;
            if (groupExport != (null)) {
                remDia = 1;
                for (remDia = 0; remDia < (diakritika.length());) {
                    novyPreklad = diakritika.substring(1, 2);
                    novyPreklad = (diakritika.substring(remDia, (remDia + 1))).toString();
                    groupExport = groupExport.replace((diakritika.substring(remDia, (remDia + 1))),
                            diakritikaBez.substring(remDia, (remDia + 1)));
                    remDia++;
                }
            } else {
                groupExport = "";
            }
            out.write(String.valueOf(groupExport) + "\t");
        }
        out.write("\n");
        for (int i = 0; i < model.getRowCount(); i++) {
            for (int j = 0; j < (model.getColumnCount()); j++) {//16
                if (model.getValueAt(i, j) == null) {
                    out.write("null" + "\t");
                } else {
                    groupExport = String.valueOf(model.getValueAt(i, j));
                    novyPreklad = groupExport;
                    if (groupExport != (null)) {
                        remDia = 1;
                        if (j == 0) {//Replace non_ACSII chars, not required in most cases
                            for (remDia = 0; remDia < (diakritika.length());) {
                                novyPreklad = diakritika.substring(1, 2);
                                novyPreklad = (diakritika.substring(remDia, (remDia + 1))).toString();
                                groupExport = groupExport.replace((diakritika.substring(remDia, (remDia + 1))),
                                        diakritikaBez.substring(remDia, (remDia + 1)));
                                remDia++;
                            }
                            out.write(34);
                            out.write(String.valueOf(groupExport));
                            out.write(34);
                            out.write("\t");
                        }  else if (j == 5) {//Number Instance
                            if (((model.getValueAt(i, j)) != null) && ((model.getValueAt(i, j)) instanceof Number)) {
                                groupExport = String.format("%.2f", (Number) (model.getValueAt(i, j)));
                            }
                            groupExport = groupExport.replace(".", ",");
                            out.write(34);
                            out.write(String.valueOf(groupExport));
                            out.write(34);
                            out.write("\t");
                        }  else if (j == 10) {//Date instance
                            groupExport = groupExport.replace("-", "/");
                            out.write(34);
                            out.write(String.valueOf(groupExport));
                            out.write(34);
                            out.write("\t");
                        } 
                    } else {
                        groupExport = "";
                        out.write(34);
                        out.write(String.valueOf(groupExport));
                        out.write(34);
                        out.write("\t");
                    }
                }
            }
            out.write("\n");
        }
        out.close();
    }
}

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

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