try { File excelFile = FileUtil.loadTemp(excel.getOriginalFilename(), excel.getInputStream()); Workbook workbook = new Workbook(excelFile.getAbsolutePath()); Worksheet firstWorksheet = workbook.getWorksheets().get(0); firstWorksheet.autoFitRows(); firstWorksheet.autoFitColumns(); if (image != null) { File imageFile = FileUtil.loadTemp(image.getOriginalFilename(), image.getInputStream()); int pictureIndex = firstWorksheet.getPictures().add(1, 1, imageFile.getAbsolutePath()); int maxRow = firstWorksheet.getCells().getMaxDataRow(); int maxColumn = firstWorksheet.getCells().getMaxDataColumn(); Picture picture = firstWorksheet.getPictures().get(pictureIndex); int pictureTopRow = maxRow + 2; int pictureLeftColumn = 0; picture.setUpperLeftRow(pictureTopRow); picture.setUpperLeftColumn(pictureLeftColumn); double rowHeight = picture.getHeightInch() * 72; if (rowHeight > 409) { rowHeight = 409; } firstWorksheet.getCells().setRowHeight(pictureTopRow, rowHeight); double pictureWidth = picture.getWidthInch() * 72; int pictureRightColumn = pictureLeftColumn + (int) (pictureWidth / 8.43); for (int column = pictureLeftColumn; column <= pictureRightColumn; column++) { firstWorksheet.getCells().setColumnWidth(column, 8.43); } } File tempFile = FileUtil.newTempFile(DateUtil.today().concat(ExcelTypeEnum.XLSX.getValue())); workbook.save(tempFile.getAbsolutePath(), com.aspose.cells.SaveFormat.XLSX); FileInputStream stream = new FileInputStream(tempFile); FileUtil.downFile(response, tempFile.getName(), stream); } catch (Exception e) { log.error("业务异常:{}", e.getMessage()); MessageException.throwMsg("业务异常"); } 我需要设置表格自适应宽度
时间: 2024-01-06 14:04:55 浏览: 104
在上述代码中,已经使用了 `firstWorksheet.autoFitRows();` 和 `firstWorksheet.autoFitColumns();` 来自适应表格的行高和列宽。如果你想要更精确地控制某一列或某几列的宽度,可以使用 `setColumnWidth` 方法,示例如下:
```
// 设置第1列的宽度为20
firstWorksheet.getCells().setColumnWidth(0, 20);
// 设置第2列到第5列的宽度为15
for (int column = 1; column <= 4; column++) {
firstWorksheet.getCells().setColumnWidth(column, 15);
}
```
可以根据需要自己调整列宽大小。注意,这里的单位是字符宽度,不是像素或者英寸。
相关问题
File mkdir = FileUtil.mkdir( preDir);
这段代码是在Java中创建一个目录,其中`FileUtil`是一个工具类,`mkdir`是一个静态方法,它接受一个文件路径字符串作为参数,并尝试在文件系统中创建该路径指定的目录。如果目录已经存在,则不做任何操作。如果创建成功,则返回一个代表该目录的`File`对象;如果创建失败,则返回`null`。`preDir`是一个字符串变量,它存储了要创建的目录的路径。
@Override public void update(TijianBean tijianBean, HttpServletRequest request) { MultipartFile file = tijianBean.getFile(); if(file.getSize()>0){ String upload = FileUtil.upload(file, request); tijianBean.setImg(upload); } tijianMapper.update(tijianBean); }
这是一个 Java 代码片段,其中使用了 Spring 框架的注解 @Override 和 HttpServletRequest 类。该方法的作用是更新一个名为 TijianBean 的实例,其中包含一个名为 file 的文件属性。如果文件大小大于 0,则将文件上传到服务器,并将上传后的文件路径设置为 TijianBean 的 img 属性。最后,调用 tijianMapper 的 update 方法更新 TijianBean 对象。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)