簡體   English   中英

如何在客戶端創建excel電子表格

[英]How to create a excel spread sheet on client side

我正在創建一個Web應用程序,我想在結果中顯示excel圖標。 當用戶點擊圖標時,它應該在客戶端機器上打開一個電子表格,其中一些數據由服務器發送(客戶端將安裝excel)。

我寫了一些代碼來從webservice在我的本地機器上創建excel。

public class App 
{
  public static void main( String[] args )
{
   try {
    URL oracle = new URL("http://someService.com");
     URLConnection yc =null;
        yc = oracle.openConnection();
            //Get the workbook instance for XLS file 

            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("Sample sheet");
             BufferedReader in = new BufferedReader(
                     new InputStreamReader(
                     yc.getInputStream()));
             String inputLine;
             int rowNum =0;
                while ((inputLine = in.readLine()) != null) {
                    Row row = sheet.createRow(rowNum++);
                    String[] coloumns = inputLine.split("\t");
                    int cellNum =0;
                    for(String coloumn: coloumns){
                        coloumn.
                           Cell cell = row.createCell(cellNum++);
                           cell.setCellValue(coloumn);
                    }
                    System.out.println(inputLine);
        } 
                in.close();  
                FileOutputStream out = 
                        new FileOutputStream(new File("C:\\libraries\\new.xls"));
                workbook.write(out);
                out.close();
                System.out.println("Excel written successfully..");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }




   }
   }

這很好用。 它只是讀取來自Web服務的數據,並在我的機器上的C:\\ libraries \\ new.xls中創建一個電子表格。

現在,如果我在網絡應用程序上,我想在客戶端計算機上打開一個電子表格。 我沒有保存表格。 只需打開數據即可。

如何使用來自Web服務的數據在客戶端計算機上打開電子表格?

編輯

這是我的新服務器代碼:

@RequestMapping(value = "/Excel")
    public void getFile(HttpServletResponse response){
        OutputStream out =null;
        try {
            out = response.getOutputStream();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        response.setContentType("application/x-ms-excel");
         try {
                URL oracle = new URL("http://someService.com");
                 URLConnection yc =null;
                    yc = oracle.openConnection();
                        //Get the workbook instance for XLS file 
                    IOUtils.copy(yc.getInputStream(),out);
                    out.flush();
                            System.out.println("Excel written successfully..");

                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

}

所以現在我運行了網絡應用程序,什么都沒發生? 我應該在前端做一些事情來調用這個流。

您可以在Spring MVC控制器中執行類似的操作,將文件發送到客戶端的計算機:

@RequestMapping(value = "/myexcelreport")
public void getFile( 
  HttpServletResponse response) {
  OutputStream out=response.getOutputStream();
  response.setContentType("application/x-ms-excel");
  ... Same code a before, just use out instead of a FileOutputStream , and don't close out.
  out.flush();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM