简体   繁体   English

为什么Apache poi在服务器上运行项目时返回空的excel文件?

[英]Why Apache poi returns empty excel file when running project on server?

In my project I am using apache-poi to let the user download excel representation of some data stored in database (kind of a report).在我的项目中,我使用 apache-poi 让用户下载存储在数据库中的某些数据的 excel 表示(一种报告)。 When testing locally everything works fine.在本地测试时一切正常。 When running on server, excel file contains only headers and no data at all.在服务器上运行时,excel 文件只包含标题,根本没有数据。 At the same time some reports return correctly excel files with data in them, but some of them return empty files with header row only.同时,一些报告正确返回包含数据的 excel 文件,但其中一些报告仅返回带有标题行的空文件。

In the project report has two ways of data representation: web-based and excel downloadable file.在项目报告中有两种数据表示方式:基于网络和excel可下载文件。 Web-based view works correctly and lets me know that report does return data.基于 Web 的视图工作正常,并让我知道报告确实返回数据。 So it is strange that excel file is empty.所以奇怪的是excel文件是空的。

The problem is that logs are empty and response code is 200 when running in server - so I don't get any error messages that could help me to solve the problem.问题是在服务器中运行时日志为空并且响应代码为 200 - 所以我没有收到任何可以帮助我解决问题的错误消息。 On the other hand running the same code locally returns me an excel with data in it - so again, I don't get any errors and can't debug.另一方面,在本地运行相同的代码会返回一个包含数据的 excel - 所以再次,我没有收到任何错误并且无法调试。

Below is the class I am using to create excel file.下面是我用来创建excel文件的类。

ExcelUtils.java

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import team.alabs.hcms.model.Header;
import team.alabs.hcms.model.Report;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelUtils {

    public static Workbook createWorkbook(Report report, List<Map<String, Object>> reportData, List<Header> headers) {
        Workbook reportBook = new XSSFWorkbook();
        String empty = new String("");
        Sheet sheet = reportBook.createSheet("report");
        Row headerRow = sheet.createRow(0);
        for (int i = 0; i < headers.size(); i++) {
            headerRow.createCell(i).setCellValue(headers.get(i).getTitle());
        }
        for (int i = 0; i < reportData.size(); i++) {
            Row row = sheet.createRow(i + 1);
            HashMap<String, Object> tmprow = (HashMap<String, Object>) reportData.get(i);
            for (int j = 0; j < headers.size(); j++) {
                Object value = tmprow.get(headers.get(j).getCode()) != null ? tmprow.get(headers.get(j).getCode()) : empty;
                row.createCell(j).setCellValue(value.toString());
            }
        }
        return reportBook;

    }

    public static byte[] getBytes(Workbook workbook) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            workbook.write(bos);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            bos.close();
        }
        byte[] bytes = bos.toByteArray();
        return bytes;
    }

}

I expect that running project on server will return excel with data inside.我希望在服务器上运行的项目将返回带有内部数据的 excel。

I had a similar issue where excel file was getting created in my local system with whole data but when it was running in server it is creating a excel with 0 (zero) bytes size.我有一个类似的问题,即在我的本地系统中使用整个数据创建了 excel 文件,但是当它在服务器中运行时,它正在创建一个 0(零)字节大小的 excel。 This is because the execution never reaches -这是因为执行永远不会到达 -

workbook.write(bos); // From your code above

And the reason is - In my case it was in multithreaded environment (using ThreadPoolTaskExecutor).原因是 - 在我的情况下,它处于多线程环境中(使用 ThreadPoolTask​​Executor)。 So this is what was the issue - Async thread was getting terminated with uncaughtexception which get caught in FutureTask but never been thrown back so the application not aware of any failure and that thread stops(ends) there.所以这就是问题所在 - 异步线程因未捕获的异常而终止,该异常在 FutureTask 中被捕获但从未被抛出,因此应用程序不知道任何失败并且该线程在那里停止(结束)。 There are ways we can enable handler for such kind of exceptions.我们可以通过多种方式为此类异常启用处理程序。 Other way is -另一种方式是——

Future<?> future = taskExecutor.submit(runnableTask);
future.get(); // This will throw an exception if the thread has thrown an uncaughtexception.

Reference link - Java FutureTask completion check参考链接 - Java FutureTask 完成检查

Based on the outcome, one can easily find the root cause.根据结果​​,很容易找到根本原因。

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM