c/s架构中,在本地端下载Excel(csv格式)到本地端的方式

 

 一、通过文件流的形式。首先FileInputStream读取Excel,然后FileOutputStream写出来,例如:(下面的代码没有实现,存在缺陷)

JFileChooser chooser = new JFileChooser();

//要想添加默认的文件名,需将setSelectedFile()这个方法放到打开对话框之前

String defaultFileName = "a.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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值