如何对齐 Excel 文件的输出?
''''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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以这样做:
输出是:
每列之间的空间由变量 spaceBetween 决定。
总之,你应该能够改变线路
System.out.print(worksheet.getCells().get(i, j).getValue() + " ");
到
您在代码中某处选择 spaceBetween 值的位置。
You can do something like this:
The output is:
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
where you choose a value for spaceBetween somewhere in the code.
如果您想要对齐控制台中打印的输出行,则必须在字段上循环两次。第一个用于获取每列的长度,第二个用于正确打印它们。
您可以使用给定
String
的方法,用空格将其填充到末尾:然后,将每个单元格的值作为二维数组
cells 中的
,执行以下操作:String
给出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:Then, given the values of each cell as a
String
in a bidimensional arraycells
, doing the following:假设您使用的是 aspose,由于您包含了标签,您可以使用导出到数组方法:
对于左对齐输出,将 print 语句更改为
当然
15
只是我选择的随机值,因为我认为单元格值的最大长度为 15 个字符。根据您的需要进行更改。您还可以对于每一列,检查该列中所有字符串的长度,并将该列中的最大字符串长度作为该列的列宽。例如,这可能会以如下形式结束:Assuming you are using aspose, since you included the tag, you could use the export to array method:
For left-aligned output change the print statement to
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: