poi excel导入导出 java

 用poi  做了一个excel导入导出的小demo,方便以后查阅

Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。

  结构:

  HSSF - 提供读写Microsoft Excel格式档案的功能。

  XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。

  HWPF - 提供读写Microsoft Word格式档案的功能。

  HSLF - 提供读写Microsoft PowerPoint格式档案的功能。

  HDGF - 提供读写Microsoft Visio格式档案的功能。

很强大的东西,不错。

 

页面代码

 1 <form  method="post"  action="importExcel.action" enctype="multipart/form-data">

 2 

 3 <table>

 4 

 5     <tr><td><input type="file" width="300px" name="myfile"/></td></tr>

 6 

 7     <tr><td><input type="file" width="300px" name="myfile"></td></tr>

 8 

 9     <tr><td><input type="file" width="300px" name="myfile"></td></tr>

10 

11     <tr><td><input type="submit" value="上传excel"></td></tr>    

12 

13     </table>

14 

15 </form>

 

 

 

 

action代码

 1 public String export2Excel(){

 2 

 3     List<Person> list = new ArrayList<Person>();

 4 

 5    

 6 

 7     Person p1 = new Person();

 8 

 9     p1.setAge(20);

10 

11     p1.setName("p1");

12 

13    

14 

15     Person p2 = new Person();

16 

17     p2.setAge(22);

18 

19     p2.setName("p2");

20 

21    

22 

23     list.add(p1);

24 

25     list.add(p2);

26 

27    

28 

29     ExcelOprate.Export2Excel(list);

30 

31     return null;

32 

33 }

34 

35    

36 

37 public String importExcel(){

38 

39     FileInputStream fin =null;

40 

41     try {

42 

43        for(int i=0;i<myfile.size();i++){

44 

45           

46 

47               fin = new FileInputStream(myfile.get(i));       

48 

49               ExcelOprate.importExcel(fin);

50 

51           

52 

53        }

54 

55     fin.close();

56 

57     } catch (Exception e) {

58 

59        e.printStackTrace();

60 

61     }

62 

63     return null;

64 

65 }

 

 

 

业务代码

 

  1 import java.io.IOException;

  2 

  3 import java.io.InputStream;

  4 

  5 import java.io.UnsupportedEncodingException;

  6 

  7 import java.net.URLEncoder;

  8 

  9 import java.util.ArrayList;

 10 

 11 import java.util.List;

 12 

 13  

 14 

 15 import javax.servlet.http.HttpServletResponse;

 16 

 17  

 18 

 19 import org.apache.poi.hssf.usermodel.HSSFCell;

 20 

 21 import org.apache.poi.hssf.usermodel.HSSFCellStyle;

 22 

 23 import org.apache.poi.hssf.usermodel.HSSFRow;

 24 

 25 import org.apache.poi.hssf.usermodel.HSSFSheet;

 26 

 27 import org.apache.poi.hssf.usermodel.HSSFWorkbook;

 28 

 29 import org.apache.poi.hssf.util.HSSFColor;

 30 

 31 import org.apache.struts2.ServletActionContext;

 32 

 33  

 34 

 35 import reflect.Person;

 36 

 37  

 38 

 39  

 40 

 41  

 42 

 43  

 44 

 45 public class ExcelOprate{

 46 

 47 public static void Export2Excel(List<Person> list) { 

 48 

 49         HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件 

 50 

 51         HSSFSheet sheet = workbook.createSheet();// 创建一个Excel的Sheet 

 52 

 53         HSSFCellStyle style = workbook.createCellStyle();

 54 

 55         style.setFillBackgroundColor(HSSFColor.BLUE_GREY.index);

 56 

 57        

 58 

 59        

 60 

 61         HSSFRow titleRow =  sheet.createRow(0);

 62 

 63         titleRow.createCell(0).setCellValue("姓名");

 64 

 65         titleRow.createCell(1).setCellValue("年龄");

 66 

 67         titleRow.setRowStyle(style);

 68 

 69        

 70 

 71        

 72 

 73        

 74 

 75         for(int i=0;i<list.size();i++){

 76 

 77         HSSFRow row =  sheet.createRow(i+1);

 78 

 79         row.createCell(0,HSSFCell.CELL_TYPE_STRING).setCellValue(list.get(i).getName());

 80 

 81         row.createCell(1,HSSFCell.CELL_TYPE_NUMERIC).setCellValue(list.get(i).getAge());

 82 

 83         }

 84 

 85        

 86 

 87         HttpServletResponse response = ServletActionContext.getResponse();

 88 

 89        

 90 

 91         response.setContentType("application/vnd.ms-excel");

 92 

 93     try {

 94 

 95            response.setHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode("person", "UTF-8") + "\"");

 96 

 97        } catch (UnsupportedEncodingException e) {

 98 

 99            e.printStackTrace();

100 

101        }

102 

103     response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");

104 

105     response.setHeader("Pragma", "public");

106 

107     response.setDateHeader("Expires", 0);

108 

109    

110 

111    

112 

113     try {

114 

115            workbook.write(response.getOutputStream()); //输出流控制workbook

116 

117            response.getOutputStream().flush();

118 

119            response.getOutputStream().close();

120 

121        } catch (IOException e) {

122 

123            e.printStackTrace();

124 

125        }

126 

127     }  

128 

129  

130 

131  

132 

133  

134 

135     public static void importExcel(InputStream  inputStream ) {

136 

137        try {

138 

139            HSSFWorkbook workbook = new HSSFWorkbook(inputStream);

140 

141           

142 

143            List<Person> list = new ArrayList<Person>();

144 

145            Person person;

146 

147           

148 

149            HSSFSheet sheet;

150 

151            HSSFRow row;

152 

153            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {

154 

155               sheet = workbook.getSheetAt(i);

156 

157               for (int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) {

158 

159                   person = new Person();

160 

161                   row = sheet.getRow(j);

162 

163                  

164 

165                   if (row.getCell(0) == null) {

166 

167                       person.setName("");

168 

169                   } else if (row.getCell(0).getCellType() == 0) {

170 

171                       person.setName(new Double(row.getCell(0).getNumericCellValue()).toString());

172 

173                   }

174 

175                   // 如果EXCEL表格中的数据类型为字符串型

176 

177                   else {

178 

179                      person.setName(row.getCell(0).getStringCellValue().trim()) ;

180 

181                   }

182 

183                  

184 

185                   if (row.getCell(1) == null) {

186 

187                      person.setAge(0);

188 

189                   } else if (row.getCell(1).getCellType() == 0) {

190 

191                      person.setAge((int)(row.getCell(1).getNumericCellValue()));

192 

193                   }

194 

195                   // 如果EXCEL表格中的数据类型为字符串型

196 

197                   else {

198 

199                       person.setAge(Integer.parseInt(row.getCell(1).getStringCellValue().trim())) ;

200 

201                   }

202 

203                  

204 

205                   list.add(person);

206 

207               }

208 

209            }

210 

211           

212 

213           

214 

215            for(Person p:list){

216 

217               System.out.println(p.getName()+":  "+p.getAge());

218 

219            }

220 

221           

222 

223        } catch (IOException e) {

224 

225            e.printStackTrace();

226 

227        }

228 

229       

230 

231     }

232 

233 } 

 

 

 

 

 

struts2配置

 

1 <action name="export2Excel" class="utilAction" method="export2Excel"></action>

2 

3 <action name="importExcel" class="utilAction" method="importExcel"></action>

 

 

 

 

 

你可能感兴趣的:(Excel)