遇到一个棘手的问题,在C/S架构中,用java实现下载Excel模板的功能,在网络查的都是jsp的方式,没有找到C/S架构的,我是用swing做的,把Excel放到了一个项目文件夹中,用户下载的时候直接下载到用户选择的路径中,该怎么实现 ,我用这种方式实现的,但是有问题
protected void loadTemplateAction_actionPerformed(ActionEvent e) throws Exception {
JFileChooser chooser = new JFileChooser();
//要想添加默认的文件名,需将setSelectedFile()这个方法放到打开对话框之前
String defaultFileName = "customers.csv";
chooser.setSelectedFile(new File(defaultFileName)); chooser.showSaveDialog(this);
FileInputStream fis = null;
FileOutputStream fos = null;
String sourcefileUrl = "ini/template1/customers.csv";
String outUrl = chooser.getCurrentDirectory().getPath();
File sourceFile = new File(sourcefileUrl);
File targetFile = new File(outUrl);
fis = new FileInputStream(sourceFile);
byte[] b = new byte[(int) sourceFile.length()];
int len = fis.read(b);
while(len>0){
fos = new FileOutputStream(targetFile);
System.out.println("len:"+len);
System.out.println("os:"+fos);
fos.write(b, 0, len);
fos.flush();
}
fis.close();
contentFileNameText.setText("模板下载成功...");
}请大家帮帮忙,谢谢了
protected void loadTemplateAction_actionPerformed(ActionEvent e) throws Exception {
JFileChooser chooser = new JFileChooser();
//要想添加默认的文件名,需将setSelectedFile()这个方法放到打开对话框之前
String defaultFileName = "customers.csv";
chooser.setSelectedFile(new File(defaultFileName)); chooser.showSaveDialog(this);
FileInputStream fis = null;
FileOutputStream fos = null;
String sourcefileUrl = "ini/template1/customers.csv";
String outUrl = chooser.getCurrentDirectory().getPath();
File sourceFile = new File(sourcefileUrl);
File targetFile = new File(outUrl);
fis = new FileInputStream(sourceFile);
byte[] b = new byte[(int) sourceFile.length()];
int len = fis.read(b);
while(len>0){
fos = new FileOutputStream(targetFile);
System.out.println("len:"+len);
System.out.println("os:"+fos);
fos.write(b, 0, len);
fos.flush();
}
fis.close();
contentFileNameText.setText("模板下载成功...");
}请大家帮帮忙,谢谢了
解决方案 »
- 快速排序 递归问题 求解。。。
- 使用了未经检查或不安全的操作,什么意思?
- 不显示删除回复显示所有回复显示星级回复显示得分回复 技术面试过后,HR多久会有通知呀?
- spring Hibernate 整合 mappingLocations 的配置问题
- 页面转向问题?急!
- 到底用不用portal
- jdk高版本到低版本移植问题
- 奇怪的Nullpointer Excepton
- 求助:通过log4j填加日志到SQL Server中
- 请教jsp如何连接mysql!
- 怎么我用hibernate生成的DAO不是继承HibernateDaoSupport
- 利用struts2<s:iterator>标签取List<Object[]>内的对象数组元素
一、通过文件流的形式。首先FileInputStream读取Excel,然后FileOutputStream写出来,例如:(下面的代码没有实现,存在缺陷)JFileChooser chooser = new JFileChooser();//要想添加默认的文件名,需将setSelectedFile()这个方法放到打开对话框之前String defaultFileName = "customers.csv";chooser.setSelectedFile(new File(defaultFileName));
chooser.showSaveDialog(this);
FileInputStream fis = null;FileOutputStream fos = null;String sourcefileUrl = "ini/template1/customers.csv";// String outUrl = chooser.getSelectedFile().getPath(); String outUrl = chooser.getCurrentDirectory().getPath();System.out.println("outUrl:"+outUrl);
File sourceFile = new File(sourcefileUrl);System.out.println("sourceFile:"+sourceFile);
File targetFile = new File(outUrl);System.out.println("targetFile:"+targetFile);
fis = new FileInputStream(sourceFile);System.out.println("fis:"+fis);
byte[] b = new byte[(int) sourceFile.length()];int len = fis.read(b);System.out.println("len:"+len);while(len>0){fos = new FileOutputStream(targetFile);System.out.println("len:"+len);System.out.println("os:"+fos);fos.write(b, 0, len);fos.flush();}
// fos.close();fis.close();contentFileNameText.setText("模板下载成功...");
二、新创建一个Excel表格,加上头标题,表面看起来实现了下载的功能,实际是新创建的Excel。//下载Excel模板,可以通过新创建的模板来实现下载 JFileChooser chooser = new JFileChooser(); String defaultFileName = "customers.csv";chooser.setSelectedFile(new File(defaultFileName));chooser.showSaveDialog(this);
File file = chooser.getSelectedFile(); HSSFWorkbook workbook = new HSSFWorkbook();HSSFSheet sheet = workbook.createSheet("第一页");
HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("客户名字"); cell = row.createCell((short) 1);
cell.setCellValue("客户类型"); cell = row.createCell((short) 2);
cell.setCellValue("热点客户"); cell = row.createCell((short) 3); ...// ByteArrayOutp;utStream baos = new ByteArrayOutputStream(); FileOutputStream fos = new FileOutputStream(file); try{// FileOutputStream fos = new FileOutputStream(file); workbook.write(fos); }catch(Exception ex){ExceptionHandler.handle(ex);}