Excel遍历图片 适应单元格大小并居中

点进去了,但是没找到申请30天的入口! 能给个具体的操作步骤吗?

@xiaoman
您可以通过在购买向导中将产品添加到购物车来申请临时许可证。
https://purchase.aspose.com/buy

或者,您可以从现有报价中申请临时许可证。
https://purchase.aspose.com/orders

自适应居中单测.zip (7.5 MB)

那个对应的问题我在导出的excel中做了标记 请帮忙看看 谢谢

@xiaoman
经过分析发现这里可能存在某些意外导致创作语言和默认字体冲突。针对你的实际情况,这里有两个解决方案希望能够对你有所帮助:
1、设置excel软件的创作语言为中文,然后再运行程序。
文件-设置-语言-创作语言和校对-中文

2、在进行你的实际操作之前添加字体设置。
参考代码如下:

原来的代码

try (InputStream inputStreamAspose = new ByteArrayInputStream(data)) {
    asposeWork = new Workbook(inputStreamAspose);
 }

替换后的代码

try (InputStream inputStreamAspose = new ByteArrayInputStream(data)) {
    asposeWork = new Workbook(inputStreamAspose);
    Style defaultStyle = asposeWork.getDefaultStyle();
    defaultStyle.getFont().setSchemeType(FontSchemeType.NONE);
    asposeWork.setDefaultStyle(defaultStyle);
 }

try (InputStream inputStreamAspose = new ByteArrayInputStream(data)) {
asposeWork = new Workbook(inputStreamAspose);
Style style = asposeWork.createStyle();
style.getFont().setName(“宋体”);
style.getFont().setSize(10);
asposeWork.setDefaultStyle(style);
}
用这种可以 你上面那个好像不行;另外,我试了几个字段 宋体效果居中最好;其他字体会有一定偏差!

宋体效果:
image.jpg (73.3 KB)

Calibri效果:
image.jpg (77.2 KB)

@xiaoman
感谢你的反馈。宽度测量和默认样式的字体有关,默认样式的字体和Excel默认字体保持一致能获得更精确的测量结果。
你可以通过以下方式获取测量时使用的默认字体:

workbook.getDefaultStyle().getFont().getName()

如果你直接更改workbook默认样式的字体,Excel文件行列的宽高就会改变,会破坏文件里原来的布局等设置。对于Excel文件,你可以直接修改默认字体,在默认字体下进行布局,最后测量得到更精确的结果。关于如何设置Excel默认字体,请查看附件。defaultFont.png (60.3 KB)

另外,宽高测量是在机器显示比例100%设置下进行测量的,如果不同的机器显示比例,可能会影响测量结果。如果你有任何疑问,请随时联系我们。

@xiaoman
你机器设置的显示比例是多少?
在Excel中打开的时候, 列宽是多少像素?

@xiaoman
请使用以下样例代码打印出行高和列宽,这对我们定位问题将会很有帮助。我们很块就会检查。

Workbook workbook = new Workbook(filePath + "订单模板.xlsx");
Worksheet sheet = workbook.getWorksheets().get(0);
RowCollection rows = sheet.getCells().getRows();
Iterator rowIter = rows.iterator();
while(rowIter.hasNext())
{
	Row row = (Row)rowIter.next();
	System.out.println("row index: " + row.getIndex() + "   height: " + row.getHeight());
}
ColumnCollection columns = sheet.getCells().getColumns();

Iterator colIter = columns.iterator();
while(colIter.hasNext())
{
	Column column = (Column)colIter.next();
	System.out.println("column index: " + column.getIndex() + "   width: " + column.getWidth());
}

模板默认字体:宋体
row index: 0 height: 23.2
row index: 1 height: 33.0
row index: 2 height: 35.0
row index: 7 height: 16.8
column index: 0 width: 12.25
column index: 1 width: 10.13
column index: 2 width: 13.13
column index: 3 width: 18.75
column index: 4 width: 25.88
column index: 5 width: 15.38
column index: 6 width: 21.75
column index: 7 width: 13.88
调整前的高度:33.000000 调整后的高度:81.000000
调整前的高度:35.000000 调整后的高度:81.000000

@xiaoman
请问你在Excel文件中看到的默认字体是什么?机器的显示比例是多少?

关于如何查看Excel文件默认字体,请查看附件。defaultFont.png (60.3 KB)

默认是宋体,但是后面根据你的截图提示调整过了默认字体,图片好像还是不能完全居中,但是下面的数据好像是没变的
row index: 0 height: 23.2
row index: 1 height: 33.0
row index: 2 height: 35.0
row index: 7 height: 16.8
column index: 0 width: 12.25
column index: 1 width: 10.13
column index: 2 width: 13.13
column index: 3 width: 18.75
column index: 4 width: 25.88
column index: 5 width: 15.38
column index: 6 width: 21.75
column index: 7 width: 13.88
另外 你说的机器的显示比例是多少 这个要怎么看?

对了 我使用的WPS软件

@xiaoman
机器的显示比例就是电脑屏幕的"缩放与布局"里设置的比例。

我把设置字体的代码注释掉了
image.jpg (41.9 KB)
image.jpg (121.6 KB)

image.jpg (51.5 KB)

@xiaoman
请使用以下样例代码打印出以像素为单位的行高和列宽,同时提供在Excel软件界面看到的行高和列宽,我们需要比较两个数据的差距。这对我们定位问题将会很有帮助。

Workbook workbook = new Workbook(filePath + "订单模板.xlsx");
Worksheet sheet = workbook.getWorksheets().get(0);
Cells cells = sheet.getCells();
RowCollection rows = sheet.getCells().getRows();
Iterator rowIter = rows.iterator();
while(rowIter.hasNext())
{
	Row row = (Row)rowIter.next();
	System.out.println("row index: " + row.getIndex() + "   height: " + cells.getRowHeightPixel(row.getIndex()));
}
ColumnCollection columns = sheet.getCells().getColumns();

Iterator colIter = columns.iterator();
while(colIter.hasNext())
{
	Column column = (Column)colIter.next();
	System.out.println("column index: " + column.getIndex() + "   width: " + cells.getColumnWidthPixel(column.getIndex()));
}

另外,正式运行的服务器和测试机器一样吗?如果不一样,你愿意提供他们的差别吗?

之前是在本地用的单测跑的,项目服务器上更奇怪的现象. 生成的文件预览打开(用的阿里巴巴 WebOffice) 居中正常 但是下载下来,用WPS就不居中了
在线预览(WebOffice):
image.jpg (115.5 KB)
下载本地(wps打开):
image.jpg (101.3 KB)

本地测试结果(本地WPS默认字体是宋体 代码没有在单独设置字体 没有调用自适应的函数):
模板默认字体:宋体
row index: 0 height: 31
row index: 1 height: 76
row index: 2 height: 72
row index: 7 height: 22
column index: 0 width: 103
column index: 1 width: 86
column index: 2 width: 110
column index: 3 width: 155
column index: 4 width: 212
column index: 5 width: 128
column index: 6 width: 179
column index: 7 width: 155
调整前的高度:57.000000 调整后的高度:57.000000
调整前的高度:54.000000 调整后的高度:54.000000

生成的文件:
image.jpg (83.8 KB)

用cells.getRowHeight cells.getColumnWidth 打印的数据
row index: 0 height: 23.2
row index: 1 height: 57.0
row index: 2 height: 54.0
row index: 7 height: 16.8
column index: 0 width: 12.25
column index: 1 width: 10.13
column index: 2 width: 13.13
column index: 3 width: 18.75
column index: 4 width: 25.88
column index: 5 width: 15.38
column index: 6 width: 21.75
column index: 7 width: 18.75
调整前的高度:57.000000 调整后的高度:57.000000
调整前的高度:54.000000 调整后的高度:54.000000

@xiaoman
请再提供一下在软件里看到的行高和列宽数据,我们需要比较两个数据的差距。这对我们定位问题将会很有帮助。

在生成的文件图片中有标注