从Rest Controller下载生成的excel文件

时间:2017-08-03 11:27:07

标签: java excel spring

我正在为这个项目使用Spring(Boot)框架。现在我正在尝试实现用户单击按钮(生成文件)的功能,这使他下载所述文件。目前我只设法在我选择的目录中生成excel文件,但这不是我想要的。我希望在用户单击按钮后开始下载excel文件。我见过许多有关此问题的帖子都有过时的解决方案,或者不符合我的需求。我在void方法上这样做,如果可能的话我不想要响应类型方法。 这是我到目前为止的代码(使用apache poi创建的excel文件):

要求服务生成文件的控制器:

 export class MenuItem extends React.Component<MenuItemProps, {}> {
 }

获取所需数据并命名文件的服务。把课叫到哪里 我实际创建了excel文件(ExcelUtils)

@GetMapping("{tableId}/generateCvlList")
public void generateCvlList(@PathVariable(value = "tableId") String tableId, String sourceLanguage, String targetLanguage) {
    vpTranslationServiceI.generateCvlList(tableId, sourceLanguage, targetLanguage); 
    LOG.info("done");
}

在R:\文件夹中创建并保存excel文件的类(我不想要):

@Override
public void generateCvlList(String tableId, String sourceLanguage, String targetLanguage) {
    List<TranslationsGeneratorModel> translationsGenModelList = vpCodeTranslationDao.getTranslationsForSourceAndTarget(tableId, sourceLanguage, targetLanguage);
    String fileName = "language_cvl_"+sourceLanguage+"_to_"+targetLanguage+".xlsx";
    try {
        ExcelUtils.createExcelFile(fileName, translationsGenModelList);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

您可以在控制器的方法中包含HttpResponse

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        android:layout_marginRight="0dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="0dp" />


    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="0dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/toolbar2"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            android:id="@+id/constraint_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/activity_horizontal_margin">

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:paddingBottom="4dp"
                android:paddingTop="4dp"
                android:text="Customer Name"
                android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                android:textColor="@color/black"
                app:layout_constraintBaseline_toBaselineOf="@+id/customer_name"
                app:layout_constraintLeft_toLeftOf="parent" />


            <EditText
                android:id="@+id/customer_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="0dp"
                android:layout_marginTop="0dp"
                android:ems="10"
                android:hint="Name"
                android:inputType="textPersonName"
                app:layout_constraintLeft_toRightOf="@+id/textView5"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
  </android.support.constraint.ConstraintLayout>

 </ScrollView>

从响应中获取输出流并传递给应该使用输出流而不是使用此

的生成方法
@GetMapping("{tableId}/generateCvlList")
public void generateCvlList(@PathVariable(value = "tableId") String tableId, String sourceLanguage, String targetLanguage, HttpResponse  response) {

您可以将excel写入响应的输出。

当报告生成需要大量时间并且您的请求可能会超时时,您可能需要文件的唯一原因。

如果生成大文件且过程耗时,则必须使用这些文件。生成文件(可以从UUID创建名称)后,将发送文件名以便稍后下载。

在这种情况下,您需要更复杂的逻辑来删除下载的文件(或在一段时间后),以通知用户有关报告创建进度等。

相关问题