如何对齐 Excel 文件的输出?

发布于 2025-01-15 03:36:47 字数 1195 浏览 3 评论 0原文

    ''''Workbook wb = new Workbook("workbook.xlsx");
    WorksheetCollection collection = wb.getWorksheets();
    
    
    for (int worksheetIndex = 0; worksheetIndex < collection.getCount(); worksheetIndex++) {
        
        Worksheet worksheet = collection.get(worksheetIndex);
        System.out.println("People on Island: " + worksheet.getName());
        System.out.println();
        
        int rows = worksheet.getCells().getMaxDataRow();
        int cols = worksheet.getCells().getMaxDataColumn();
        Style st = wb.createStyle();
        
        for (int i = 0; i <= rows; i++) {
            
        for (int j = 0; j <= cols; j++)  {
            
            System.out.print(worksheet.getCells().get(i, j).getValue() + "   ");
            
            
        }
            
        System.out.println();
        System.out.println();
        }
        
    }
}''''

当前输出:

岛上人员:Sheet1

名字 姓氏 性别 国家/地区 年龄 日期

Dulce Bleoop 女 津巴布韦 43 2035-03-01T00:00:00

Sheeba Loadlee 女 Sketchawan 24 3040-04-03T00:00:00

Jello Bamop 男 南极洲 32 2053-04-06T00:00:00

芭芭拉·斯莱克 女性 Scandalousia 70 3203-03-12T00:00:00

    ''''Workbook wb = new Workbook("workbook.xlsx");
    WorksheetCollection collection = wb.getWorksheets();
    
    
    for (int worksheetIndex = 0; worksheetIndex < collection.getCount(); worksheetIndex++) {
        
        Worksheet worksheet = collection.get(worksheetIndex);
        System.out.println("People on Island: " + worksheet.getName());
        System.out.println();
        
        int rows = worksheet.getCells().getMaxDataRow();
        int cols = worksheet.getCells().getMaxDataColumn();
        Style st = wb.createStyle();
        
        for (int i = 0; i <= rows; i++) {
            
        for (int j = 0; j <= cols; j++)  {
            
            System.out.print(worksheet.getCells().get(i, j).getValue() + "   ");
            
            
        }
            
        System.out.println();
        System.out.println();
        }
        
    }
}''''

OUTPUT CURRENTLY:

People on Island: Sheet1

First Name Last name Gender Country Age Date

Dulce Bleoop Female Zimbabwe 43 2035-03-01T00:00:00

Sheeba Loadlee Female Sketchawan 24 3040-04-03T00:00:00

Jello Bamop Male Antarctica 32 2053-04-06T00:00:00

Barbara Slack Female Scandalousia 70 3203-03-12T00:00:00

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海之角 2025-01-22 03:36:47

你可以这样做:

//Code for reading data, not relevant for the solution
    String my_string= """
            People on Island: Sheet1

            FirstName LastName Gender Country Age Date

            Dulce Bleoop Female Zimbabwe 43 2035-03-01T00:00:00

            Sheeba Loadlee Female Sketchawan 24 3040-04-03T00:00:00

            Jello Bamop Male Antarctica 32 2053-04-06T00:00:00

            Barbara Slack Female Scandalousia 70 3203-03-12T00:00:00""";

    String[] split_string = my_string.split("\n\n");

    String[][] finalString = new String[split_string.length][];

    for (int i = 0; i < split_string.length; i++) {
        finalString[i] = split_string[i].split(" ");
    }
//Relevant code below
    int spaceBetween = 25; //this variable decides the distance between each column
    for (int i = 0; i < finalString.length; i++) {
        for (int j = 0; j < finalString[i].length; j++)  {
            System.out.print(finalString[i][j]);
            for (int k = 0; k < spaceBetween-finalString[i][j].length(); k++)
            System.out.print(" ");
        }
        System.out.println("\n");
    }

输出是:

People                   on                       Island:                  Sheet1                   

FirstName                LastName                 Gender                   Country                  Age                      Date                     

Dulce                    Bleoop                   Female                   Zimbabwe                 43                       2035-03-01T00:00:00      

Sheeba                   Loadlee                  Female                   Sketchawan               24                       3040-04-03T00:00:00      

Jello                    Bamop                    Male                     Antarctica               32                       2053-04-06T00:00:00      

Barbara                  Slack                    Female                   Scandalousia             70                       3203-03-12T00:00:00    

每列之间的空间由变量 spaceBetween 决定。

总之,你应该能够改变线路
System.out.print(worksheet.getCells().get(i, j).getValue() + " ");

val = worksheet.getCells().get(i, j).getValue();
System.out.print(val);
for (int k = 0; k < spaceBetween-val.lenght(); k++)
    System.out.print(" ");

您在代码中某处选择 spaceBetween 值的位置。

You can do something like this:

//Code for reading data, not relevant for the solution
    String my_string= """
            People on Island: Sheet1

            FirstName LastName Gender Country Age Date

            Dulce Bleoop Female Zimbabwe 43 2035-03-01T00:00:00

            Sheeba Loadlee Female Sketchawan 24 3040-04-03T00:00:00

            Jello Bamop Male Antarctica 32 2053-04-06T00:00:00

            Barbara Slack Female Scandalousia 70 3203-03-12T00:00:00""";

    String[] split_string = my_string.split("\n\n");

    String[][] finalString = new String[split_string.length][];

    for (int i = 0; i < split_string.length; i++) {
        finalString[i] = split_string[i].split(" ");
    }
//Relevant code below
    int spaceBetween = 25; //this variable decides the distance between each column
    for (int i = 0; i < finalString.length; i++) {
        for (int j = 0; j < finalString[i].length; j++)  {
            System.out.print(finalString[i][j]);
            for (int k = 0; k < spaceBetween-finalString[i][j].length(); k++)
            System.out.print(" ");
        }
        System.out.println("\n");
    }

The output is:

People                   on                       Island:                  Sheet1                   

FirstName                LastName                 Gender                   Country                  Age                      Date                     

Dulce                    Bleoop                   Female                   Zimbabwe                 43                       2035-03-01T00:00:00      

Sheeba                   Loadlee                  Female                   Sketchawan               24                       3040-04-03T00:00:00      

Jello                    Bamop                    Male                     Antarctica               32                       2053-04-06T00:00:00      

Barbara                  Slack                    Female                   Scandalousia             70                       3203-03-12T00:00:00    

The Space between each column is decided by the variable spaceBetween.

In conclusion, you should be able to change the line
System.out.print(worksheet.getCells().get(i, j).getValue() + " ");
to

val = worksheet.getCells().get(i, j).getValue();
System.out.print(val);
for (int k = 0; k < spaceBetween-val.lenght(); k++)
    System.out.print(" ");

where you choose a value for spaceBetween somewhere in the code.

最佳男配角 2025-01-22 03:36:47

如果您想要对齐控制台中打印的输出行,则必须在字段上循环两次。第一个用于获取每列的长度,第二个用于正确打印它们。

您可以使用给定 String 的方法,用空格将其填充到末尾:

public static String alignLeft(String str, int width) {
    if (str.length() >= width) {
        return str;
    }
    return alignLeft(str + " ", width);
}

然后,将每个单元格的值作为二维数组 cells 中的 String 给出,执行以下操作:

int[] columnsWidths = new int[cells[0].length];
for (int i = 0; i < cells.length; i++) {
    for (int j = 0; j < cells[i].length; j++) {
        if (columnsWidths[j] < cells[i][j].length()) {
            columnsWidths[j] = cells[i][j].length();
        }
    }
}

for (int i = 0; i < cells.length; i++) {
    for (int j = 0; j < cells[i].length; j++) {
        System.out.print(alignLeft(cells[i][j], columnsWidths[j]) + " ");
    }
    System.out.println();
}

If what you want is to align the output lines printed in console, you will have to loop over the fields two times. The first for getting whe length of each column and the second to print them correctly.

You can use a method that given a String pads it with whitespaces to the end:

public static String alignLeft(String str, int width) {
    if (str.length() >= width) {
        return str;
    }
    return alignLeft(str + " ", width);
}

Then, given the values of each cell as a String in a bidimensional array cells, doing the following:

int[] columnsWidths = new int[cells[0].length];
for (int i = 0; i < cells.length; i++) {
    for (int j = 0; j < cells[i].length; j++) {
        if (columnsWidths[j] < cells[i][j].length()) {
            columnsWidths[j] = cells[i][j].length();
        }
    }
}

for (int i = 0; i < cells.length; i++) {
    for (int j = 0; j < cells[i].length; j++) {
        System.out.print(alignLeft(cells[i][j], columnsWidths[j]) + " ");
    }
    System.out.println();
}
微凉徒眸意 2025-01-22 03:36:47

假设您使用的是 aspose,由于您包含了标签,您可以使用导出到数组方法:

....

int rows = worksheet.getCells().getMaxDataRow();
int cols = worksheet.getCells().getMaxDataColumn();

Object dataTable[][] = worksheet.getCells().exportArray(0, 0, rows, cols);

for (final Object[] row : dataTable) {
    System.out.format("%15s%15s%15s%15s%15s%15s%n", row);
}

对于左对齐输出,将 print 语句更改为

 System.out.format("%-15s%-15s%-15s%-15s%-15s%-15s%n", row);

当然 15 只是我选择的随机值,因为我认为单元格值的最大长度为 15 个字符。根据您的需要进行更改。您还可以对于每一列,检查该列中所有字符串的长度,并将该列中的最大字符串长度作为该列的列宽。例如,这可能会以如下形式结束:

System.out.format("%-10s%-10s%-8s%-15s%-5s%-25s%n", row);

 

Assuming you are using aspose, since you included the tag, you could use the export to array method:

....

int rows = worksheet.getCells().getMaxDataRow();
int cols = worksheet.getCells().getMaxDataColumn();

Object dataTable[][] = worksheet.getCells().exportArray(0, 0, rows, cols);

for (final Object[] row : dataTable) {
    System.out.format("%15s%15s%15s%15s%15s%15s%n", row);
}

For left-aligned output change the print statement to

 System.out.format("%-15s%-15s%-15s%-15s%-15s%-15s%n", row);

Of course 15 is just a random value I choosed, because I thought the max length of a cell value has 15 chars. change as you see fit. You could also for each column, check length of all strings in that column and take max string length in that column as column width for that column. Which could end for example in something like:

System.out.format("%-10s%-10s%-8s%-15s%-5s%-25s%n", row);

 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文