結果に Excel アイコンを表示する Web アプリを作成しています。ユーザーがアイコンをクリックすると、サーバーから送信されたデータを含むスプレッドシートがクライアント マシンで開かれます (クライアントには Excel がインストールされています)。
Web サービスからローカル マシンで 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 アプリを使用している場合、クライアント マシンでスプレッド シートを開きたいとします。シートを保存する必要はありません。データで開くだけです。
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();
}
}
今、私はウェブアプリを実行しましたが、何も起こりませんでしたか? このストリームを呼び出すために、フロントエンドで何かをする必要があります。