0

在我的文件编写器中,我有这些代码, 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");
    }

问题是在打印了带有数字的那一列之后,将不会打印下一列..例如

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

它跳过带有数字的列旁边的列...

当我删除代码中的第二个“\”时,该列的其余部分连接到带有数字的列。

知道我该如何解决这个问题吗?提前致谢

4

3 回答 3

2

第二个\必须写在\t我猜之前。

等等...我创建了一个带有文本列和数字列的 Excel 工作表。然后我将该工作表导出到一个制表符分隔的文件中。当我在文本编辑器中打开该文件时,数字不会以任何形式转义。所以我认为你不需要这个if

于 2012-11-30T09:17:27.963 回答
2

你可能的意思是:

excelWriter.write("\"" + model.getValueAt(i, j) + "\"\t");
于 2012-11-30T09:22:15.320 回答
2

例如,文件可以使用 .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();
    }
}
于 2012-11-30T09:25:12.450 回答