首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >代码样例:JAVA+POI读写xlsx的Excel表格

代码样例:JAVA+POI读写xlsx的Excel表格

作者头像
软件测试大叔
发布2026-04-22 15:34:46
发布2026-04-22 15:34:46
630
举报

首先是maven配置

代码语言:javascript
复制
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.7</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.7</version>
    </dependency>

接下来是代码样例

代码语言:javascript
复制
import org.apache.poi.ss.usermodel.Cell;
import org.testng.annotations.Test;
import pages.DeepLTransPage;
import org.apache.poi.xssf.usermodel.*;
import java.io.*;

public class TestCase_DeepLTrans {
    @Test
    public void testDeepLtrans() {
        try {
            DeepLTransPage deepl = new DeepLTransPage();
            String filePath = "D:\\pyws\\TextTranslation\\TextTranslation\\text.xlsx";
            FileInputStream intputStream = new FileInputStream(filePath);
            XSSFWorkbook  workbook = new XSSFWorkbook(intputStream);
            XSSFSheet  sheet;
            intputStream.close();
            sheet = workbook.getSheetAt(0);
            FileOutputStream out = new FileOutputStream(filePath);
            int rowNum = sheet.getLastRowNum();
            XSSFRow row = null;
            for (int r = 1; r <= rowNum; r++) {
                row = sheet.getRow(r);
                String v = row.getCell(1).toString();
                String dest = row.getCell(6).toString();
                if (dest.isEmpty()){
                    log.info("当前处理第 "+r+" 行");
                    log.info("原文为:"+ v);
                    String result = deepl.testDeepL(v);
                    XSSFCell cell= row.createCell(6,Cell.CELL_TYPE_STRING);
                    cell.setCellValue(result);
                }

            }
            out.flush();
            workbook.write(out);
            out.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2026-04-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档