كيف يمكنني الحصول على تيار المدخلات من كائن HSSFWorkbook

StackOverflow https://stackoverflow.com/questions/378883

  •  22-08-2019
  •  | 
  •  

سؤال

وأريد بلدي مستخدمي تطبيقات الويب لتحميل بعض البيانات كملف Excel.

ولدي وظيفة القادمة لإرسال تيار الإدخال في كائن استجابة.

public static void sendFile(InputStream is, HttpServletResponse response) throws IOException {
        BufferedInputStream in = null;
        try {
            int count;
            byte[] buffer = new byte[BUFFER_SIZE];
            in = new BufferedInputStream(is);
            ServletOutputStream out = response.getOutputStream();
            while(-1 != (count = in.read(buffer)))
                out.write(buffer, 0, count);
            out.flush();            
        }   catch (IOException ioe) { 
            System.err.println("IOException in Download::sendFile"); 
            ioe.printStackTrace();
        } finally {
            if (in != null) {
                try { in.close(); 
                } catch (IOException ioe) { ioe.printStackTrace(); }
            }   
        }
    }

وأود أن تحويل كائن HSSFWorkbook جهدي لتيار المدخلات وتمريرها إلى الطريقة السابقة.

public InputStream generateApplicationsExcel() {
    HSSFWorkbook wb = new HSSFWorkbook();
    // Populate the excel object
    return null; // TODO. return the wb as InputStream 
}

http://poi.apache.org/ apidocs / المنظمة / اباتشي / البوي / hssf / usermodel / HSSFWorkbook.html

هل كانت مفيدة؟

المحلول

والمشكلة مع سؤالك هو أنك خلط OutputStreams وInputStreams. وInputStream شيء تقرأه من وOutputStream شيء تكتب.

وهذه هي الطريقة التي أكتب كائن POI إلى دفق إخراج.

// this part is important to let the browser know what you're sending
response.setContentType("application/vnd.ms-excel");
// the next two lines make the report a downloadable file;
// leave this out if you want IE to show the file in the browser window
String fileName = "Blah_Report.xls";
response.setHeader("Content-Disposition", "attachment; filename=" + fileName); 

// get the workbook from wherever
HSSFWorkbook wb = getWorkbook();
OutputStream out = response.getOutputStream();
try {
   wb.write(out);
}       
catch (IOException ioe) { 
  // if this happens there is probably no way to report the error to the user
  if (!response.isCommited()) {
    response.setContentType("text/html");
    // show response text now
  }
}

إذا أردت إعادة استخدام التعليمات البرمجية الموجودة وكنت قد لتخزين البيانات POI في مكان ما ثم تحويل ذلك إلى دفق الإدخال. يهمني أن يتم ذلك بسهولة عن طريق كتابة هذا التقرير إلى ByteArrayOutputStream، ثم قراءة تلك بايت باستخدام ByteArrayInputStream، لكنني لن أوصي به. أن الطريقة الموجودة لديك أن يكون أكثر فائدة بمثابة تنفيذ الأنابيب عام، حيث يمكنك الأنابيب البيانات من InputStream لوOutputStream، ولكنك لا تحتاج إليها لكتابة الأشياء POI.

نصائح أخرى

ويمكنك إنشاء InputStream من كائن.

public InputStream generateApplicationsExcel() {
    HSSFWorkbook wb = new HSSFWorkbook();
    // Populate a InputStream from the excel object
    return new ByteArrayInputStream(excelFile.getBytes());
}

وأعتقد أنني أفهم ما نحاول القيام به (ربما أنا undershooting، على الرغم من)

وأنت لا تحتاج حقا أن الكثير من كود - تحقق من طريقة الكتابة -

HSSFWorkbook wb = new HSSFWorkBook();
//populate

ServletOutputStream out = response.getOutputStream();
try {
   wb.write(out);
   out.flush();
}       
catch (IOException ioe) { 
   //whatever
}
out.close();

وكما بقدر أتذكر عندما كنت أعمل ث / POI هذا ما فعلته. إذا كنت داخل إطار الشبكة قد تضطر إلى finaggle بحيث الإطار لا تحاول أن تفعل شيئا مع أن ServletOutputStream بعد أن كنت قد إغلاقه. إذا كان يحاول، ستحصل على رمي استثناء أقول لك أن دفق إخراج مغلق بالفعل.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top