好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java动态导出excel压缩成zip下载的方法

本文实例为大家分享了 java 动态导出 excel 压缩成 zip 下载 的具体代码,供大家参考,具体内容如下

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

package pack.java.io.demo;

import java.io.bufferedoutputstream;

import java.io.file;

import java.io.fileinputstream;

import java.io.fileoutputstream;

import java.io.ioexception;

import java.text.simpledateformat;

import java.util.date;

import java.util.zip.zipentry;

import java.util.zip.zipoutputstream;

import jxl.workbook;

import jxl.format.alignment;

import jxl.format.border;

import jxl.format.borderlinestyle;

import jxl.format.colour;

import jxl.format.underlinestyle;

import jxl.format.verticalalignment;

import jxl.write.label;

import jxl.write.writablecellformat;

import jxl.write.writablefont;

import jxl.write.writablesheet;

import jxl.write.writableworkbook;

import jxl.write.writeexception;

import jxl.write.biff.rowsexceededexception;

 

/**

  * zip压缩文件实例

  * add by 周海涛

  * @author administrator

  *

  */

public class zipdemo {

 

  /**

  * @param args

  * @throws ioexception

  * @throws writeexception

  * @throws rowsexceededexception

  */

  public static void main(string[] args) throws rowsexceededexception, writeexception, ioexception {

  string path = "c:/document/excel" ;

  //创建文件夹;

  createfile(path);

  //创建excel文件;

  createexcelfile(path);

  //生成.zip文件;

  craetezippath(path);

  //删除目录下所有的文件;

  file file = new file(path);

  //删除文件;

  deleteexcelpath(file);

  //重新创建文件;

  file.mkdirs();

  }

 

  /**

  * 创建文件夹;

  * @param path

  * @return

  */

  public static string createfile(string path){

  file file = new file(path);

  //判断文件是否存在;

  if (!file.exists()){

   //创建文件;

   boolean bol = file.mkdirs();

   if (bol){

   system.out.println(path+ " 路径创建成功!" );

   } else {

   system.out.println(path+ " 路径创建失败!" );

   }

  } else {

   system.out.println(path+ " 文件已经存在!" );

  }

  return path;

  }

 

  /**

  * 在指定目录下创建excel文件;

  * @param path

  * @throws ioexception

  * @throws writeexception

  * @throws rowsexceededexception

  */

  public static void createexcelfile(string path) throws ioexception, rowsexceededexception, writeexception{

  for ( int i = 0 ;i< 3 ;i++){

   //创建excel;

   writableworkbook workbook = workbook.createworkbook( new file(path+ "/" + new simpledateformat( "yyyymmddhhmmsss" ).format( new date() )+ "_" +(i+ 1 )+ ".xls" ));

   //创建第一个sheet文件;

   writablesheet sheet = workbook.createsheet( "导出excel文件" , 0 );

   //设置默认宽度;

   sheet.getsettings().setdefaultcolumnwidth( 30 );

  

   //设置字体;

   writablefont font1 = new writablefont(writablefont.arial, 14 ,writablefont.bold, false ,underlinestyle.no_underline,colour.red);

 

   writablecellformat cellformat1 = new writablecellformat(font1);

   //设置背景颜色;

   cellformat1.setbackground(colour.blue_grey);

   //设置边框;

   cellformat1.setborder(border.all, borderlinestyle.dash_dot);

   //设置自动换行;

   cellformat1.setwrap( true );

   //设置文字居中对齐方式;

   cellformat1.setalignment(alignment.centre);

   //设置垂直居中;

   cellformat1.setverticalalignment(verticalalignment.centre);

   //创建单元格

   label label1 = new label( 0 , 0 , "第一行第一个单元格(测试是否自动换行!)" ,cellformat1);

   label label2 = new label( 1 , 0 , "第一行第二个单元格" ,cellformat1);

   label label3 = new label( 2 , 0 , "第一行第三个单元格" ,cellformat1);

   label label4 = new label( 3 , 0 , "第一行第四个单元格" ,cellformat1);

   //添加到行中;

   sheet.addcell(label1);

   sheet.addcell(label2);

   sheet.addcell(label3);

   sheet.addcell(label4);

  

   //给第二行设置背景、字体颜色、对齐方式等等;

   writablefont font2 = new writablefont(writablefont.arial, 14 ,writablefont.no_bold, false ,underlinestyle.no_underline,colour.blue2);

   writablecellformat cellformat2 = new writablecellformat(font2);

   cellformat2.setalignment(alignment.centre);

   cellformat2.setbackground(colour.pink);

   cellformat2.setborder(border.all, borderlinestyle.thin);

   cellformat2.setwrap( true );

 

   //创建单元格;

   label label11= new label( 0 , 1 , "第二行第一个单元格(测试是否自动换行!)" ,cellformat2);

   label label22 = new label( 1 , 1 , "第二行第二个单元格" ,cellformat2);

   label label33 = new label( 2 , 1 , "第二行第三个单元格" ,cellformat2);

   label label44 = new label( 3 , 1 , "第二行第四个单元格" ,cellformat2);

 

   sheet.addcell(label11);

   sheet.addcell(label22);

   sheet.addcell(label33);

   sheet.addcell(label44);

 

   //写入excel表格中;

   workbook.write();

   //关闭流;

   workbook.close();

  }

  }

 

  /**

  * 生成.zip文件;

  * @param path

  * @throws ioexception

  */

  public static void craetezippath(string path) throws ioexception{

  zipoutputstream zipoutputstream = null ;

  file file = new file(path+ new simpledateformat( "yyyymmddhhmmss" ).format( new date())+ ".zip" );

  zipoutputstream = new zipoutputstream( new bufferedoutputstream( new fileoutputstream(file)));

  file[] files = new file(path).listfiles();

  fileinputstream fileinputstream = null ;

  byte [] buf = new byte [ 1024 ];

  int len = 0 ;

  if (files!= null && files.length > 0 ){

   for (file excelfile:files){

   string filename = excelfile.getname();

   fileinputstream = new fileinputstream(excelfile);

   //放入压缩zip包中;

   zipoutputstream.putnextentry( new zipentry(path + "/" +filename));

  

   //读取文件;

   while ((len=fileinputstream.read(buf)) > 0 ){

    zipoutputstream.write(buf, 0 , len);

   }

   //关闭;

   zipoutputstream.closeentry();

   if (fileinputstream != null ){

    fileinputstream.close();

   }

   }

  }

 

  if (zipoutputstream != null ){

   zipoutputstream.close();

  }

  }

 

  /**

  * 删除目录下所有的文件;

  * @param path

  */

  public static boolean deleteexcelpath(file file){

  string[] files = null ;

  if (file != null ){

   files = file.list();

  }

 

  if (file.isdirectory()){

   for ( int i = 0 ;i<files.length;i++){

   boolean bol = deleteexcelpath( new file(file,files[i]));

   if (bol){

    system.out.println( "删除成功!" );

   } else {

    system.out.println( "删除失败!" );

   }

   }

  }

  return file.delete();

  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/hongwangzhang/article/details/50764534

查看更多关于java动态导出excel压缩成zip下载的方法的详细内容...

  阅读:60次