intitle java_Java Row.setHeight方法代码示例

这篇博客展示了如何在Java中使用Apache POI库的Row.setHeight方法来设置Excel表格行的高度。通过示例代码,解释了如何在达到行数限制或Excel最大行数时初始化新的工作表,并在写入数据时处理空行的情况。
摘要由CSDN通过智能技术生成

import org.apache.poi.ss.usermodel.Row; //导入方法依赖的package包/类

/**

* 写入指定数据

*

* @param data 指定被写入的数据

*/

private void writePerData(T data) {

if (this.limit > 0 && (this.currentRowInSheet + (isSkipBlankRow && isBlankLastRow ? 0 : 1)) >= this.limit) {

//用户开启了限制,并且此Sheet已经写入了允许的最大行数则换页

this.initSheet();

} else if (this.limit <= 0 &&

(this.currentRowInSheet + (isSkipBlankRow && isBlankLastRow ? 0 : 1)) >= ROW_MOST) {

//若用户没有开启限制,但是此页写入已经超过了Excel规定的最大行数,强制换页

this.initSheet();

}

//将data转换成Map数据结构

Map mapBean = null;

if (data instanceof Map) {

mapBean = (Map) data;

} else {

mapBean = CommonUtils.toMap(data);

}

if (mapBean == null) {

throw new WriteExcelRuntimeException("Bean转换为Map时发生错误");

}

if (this.properties == null || this.properties.isEmpty()) {

throw new WriteExcelRuntimeException("properties属性不能为空");

}

if (!this.isSkipBlankRow) {

//未开启跳过空行设置,即需要写入空行

this.currentRowInSheet++;

Row row = this.currentSheetPO.createRow(this.currentRowInSheet);

row.setHeight(this.rowHeight < 0 ? -1 : this.rowHeight);

if (this.writePerRow(row, mapBean)) {

this.realRowInSheet++;

this.realRowInExcel++;

}

} else {

//开启跳过空行设置,即不写入空行

if (!this.isBlankLastRow) {

//若上一行不是空行则指针下移一行

this.currentRowInSheet++;

//创建一个新的Row

Row thisRow = this.currentSheetPO.createRow(this.currentRowInSheet);

thisRow.setHeight(this.rowHeight < 0 ? -1 : this.rowHeight);

//开始将数据写入该行

if (this.writePerRow(thisRow, mapBean)) {

//若写入了数据

this.realRowInSheet++;

this.realRowInExcel++;

//此行不是空行

this.isBlankLastRow = false;

} else {

//若没有写入任何数据,此行标记为空行

this.isBlankLastRow = true;

}

this.lastRow = thisRow;

} else {

//若上一行为空行则直接在上一行写入数据

if (this.writePerRow(this.lastRow, mapBean)) {

//若写入了数据

this.realRowInSheet++;

this.realRowInExcel++;

this.isBlankLastRow = false;

} else {

//若没有写入任何数据,此行还是标记为空行

this.isBlankLastRow = true;

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于Java OSS SDK实现的文件分片上传并返回进度条的代码示例: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.aliyun.oss.ClientConfiguration; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.model.CompleteMultipartUploadRequest; import com.aliyun.oss.model.CompleteMultipartUploadResult; import com.aliyun.oss.model.InitiateMultipartUploadRequest; import com.aliyun.oss.model.InitiateMultipartUploadResult; import com.aliyun.oss.model.PartETag; import com.aliyun.oss.model.UploadPartRequest; import com.aliyun.oss.model.UploadPartResult; public class OSSUploader { private static final String endpoint = "yourEndpoint"; private static final String accessKeyId = "yourAccessKeyId"; private static final String accessKeySecret = "yourAccessKeySecret"; private static final String bucketName = "yourBucketName"; private static final String objectName = "yourObjectName"; private static final int partSize = 1024 * 1024 * 5; // 分片大小 public static void main(String[] args) throws IOException { // 初始化OSS客户端 ClientConfiguration conf = new ClientConfiguration(); conf.setConnectionTimeout(5000); conf.setMaxErrorRetry(3); OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, conf); // 初始化分片上传 InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, objectName); InitiateMultipartUploadResult initResult = ossClient.initiateMultipartUpload(initRequest); String uploadId = initResult.getUploadId(); // 计算文件分片数 File file = new File("yourFilePath"); long fileLength = file.length(); int partCount = (int) Math.ceil((double) fileLength / partSize); // 上传分片并记录分片上传结果 List<PartETag> partETags = new ArrayList<>(); for (int i = 0; i < partCount; i++) { FileInputStream fis = new FileInputStream(file); fis.skip(i * partSize); UploadPartRequest uploadPartRequest = new UploadPartRequest(); uploadPartRequest.setBucketName(bucketName); uploadPartRequest.setKey(objectName); uploadPartRequest.setUploadId(uploadId); uploadPartRequest.setInputStream(fis); uploadPartRequest.setPartSize(partSize); uploadPartRequest.setPartNumber(i + 1); UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest); partETags.add(uploadPartResult.getPartETag()); fis.close(); // 计算上传进度并输出 double progress = (double) (i + 1) / partCount; System.out.println("Upload progress: " + progress); } // 完成分片上传 CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest(bucketName, objectName, uploadId, partETags); CompleteMultipartUploadResult completeResult = ossClient.completeMultipartUpload(completeRequest); System.out.println("Upload complete!"); // 关闭OSS客户端 ossClient.shutdown(); } } ``` 该代码示例中,我们使用了阿里云Java OSS SDK来实现文件分片上传。在初始化分片上传时,我们需要调用`ossClient.initiateMultipartUpload()`方法来初始化一个分片上传任务,并获取`uploadId`。在上传分片时,我们需要计算文件的分片数,并循环执行`ossClient.uploadPart()`方法来上传每个分片,并记录每个分片上传的结果。在完成分片上传时,我们需要调用`ossClient.completeMultipartUpload()`方法来完成分片上传任务。 在上传分片时,我们还计算了上传进度,并输出到控制台。这里的上传进度是指已上传的分片数占总分片数的比例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值