Java Method for Writing Data to XLSX Files Using Apache POI
This article presents a Java method that uses Apache POI to write data into XLSX files, detailing the implementation steps, code example, and practical considerations for generating test reports with Excel in automated testing environments.
The author, while learning Selenium and UiAutomator, explored Excel file manipulation and previously wrote a solution for reading Excel; now shares a method for writing Excel files using a Map<Integer,List<String[]>> as the data source.
//写入xlsx文档
public static void writeXlsx(String filename, Map<Integer,List<String[]>> map) {
String fileType = filename.substring(filename.lastIndexOf(".") + 1, filename.length()); //提取文件名后缀
try {
if (!fileType.equals("xlsx")) { //判断文件名是否正确
output("文件名错误!");
}
XSSFWorkbook wb = new XSSFWorkbook(); //新建工作区
for(int sheetnum=0; sheetnum
list = map.get(sheetnum+1); //取出需要写入的表格内容,这里需要+1才行
for(int i=0; iThe implementation borrows ideas from earlier work but has been re‑written and optimized to suit the author's specific requirements.
Excel is primarily used here to generate test reports; while simple formatting works, more complex features like merged cells and embedded objects become cumbersome, leading the author to eventually abandon this approach.
Readers are invited to view the original article and join a QQ group for further discussion.
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.