OutputStream os= response.getOutputStream();
WritableWorkbook wBook=Workbook.createWorkbook(os);
WritableSheet sheet=wBook.createSheet(sheetName, 0);
//设置字体
WritableFont font = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
WritableCellFormat cFormat = new WritableCellFormat(font);
cFormat.setWrap(false);
cFormat.setAlignment(Alignment.CENTRE);
//设置背景色
cFormat.setBackground(Colour.LIGHT_ORANGE);
cFormat.setWrap(true);
sheet.getSettings().setDefaultColumnWidth(12);
//表头
for(int i=0;i
}
for(int i = 0; i < contents.size(); i++){
String[] content = (String[]) contents.get(i);
for (int j = 0; j < content.length; j++) {
sheet.addCell(new Label(j, i+1, content[j] == null " " : content[j]));
}
}
wBook.write();
wBook.close();
os.close();
}
==========================================================================
/**
* 解压缩
* @author he
* 2009-10-19
* @param fileAllPath 文件全路径名
* @throws IOException
*/
public static void unZip(String fileAllPath) throws IOException{
int index=-1;
if(fileAllPath.lastIndexOf("/")>=0){
index=fileAllPath.lastIndexOf("/");
}else{
index=fileAllPath.lastIndexOf("//");
}
String filePath=fileAllPath.substring(0,index+1);
ZipFile zipFile = new ZipFile(fileAllPath);
Enumeration emu = zipFile.entries();
int i=0;
while(emu.hasMoreElements()){
ZipEntry entry = (ZipEntry)emu.nextElement();
//会把目录作为一个file读出一次,所以只建立目录就可以,之下的文件还会被迭代到。
if (entry.isDirectory())
{
new File(filePath + entry.getName()).mkdirs();
continue;
}
BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
File file = new File(filePath + entry.getName());
//加入这个的原因是zipfile读取文件是随机读取的,这就造成可能先读取一个文件
//而这个文件所在的目录还没有出现过,所以要建出目录来。
File parent = file.getParentFile();
if(parent != null && (!parent.exists())){
parent.mkdirs();
}
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos,BUFFER);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1)
{
bos.write(data, 0, count);
}
bos.flush();
bos.close();
bis.close();
}
zipFile.close();
}
=============================================================================】
=============================================================================】
package test;
import java.io.*;
// 文件读写
public class FileStreamDemo {
public static void main(String[] args) {
try {
byte[] buffer = new byte[1024];
// 来源文件
FileInputStream fileInputStream =
new FileInputStream(new File("D://FileStreamDemo1.txt"));
// 目的文件
FileOutputStream fileOutputStream =
new FileOutputStream(new File("D://FileStreamDemo2.txt"));
//构建方法的第二个append参数如果设置为true,在开启流时如果文件不存在则会新建一个文件,如果文件存在就直接开启流,并将写入的数据附加至//文件末端。
// FileOutputStream fileOutputStream =
// new FileOutputStream(new File("D://FileStreamDemo2.txt"), true);
// available()可取得未读取的数据长度
System.out.println("复制文件:" +
fileInputStream.available() + "字节");
while(true) {
if(fileInputStream.available() < 1024) {
// 剩余的数据比1024字节少
// 一位一位读出再写入目的文件
int remain = -1;
while((remain = fileInputStream.read()) != -1) {
System.out.println(remain);
fileOutputStream.write(remain);
}
break;
} else {
// 从来源文件读取数据至缓冲区
fileInputStream.read(buffer);
// 将数组数据写入目的文件
fileOutputStream.write(buffer);
}
}
// 关闭流
fileInputStream.close()