1. package com.zuidaima.excel.util;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.util.ArrayList;
  11. import java.util.Calendar;
  12. import java.util.Date;
  13. import java.util.Map;
  14.  
  15. import org.apache.poi.hssf.usermodel.HSSFCell;
  16. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  17. import org.apache.poi.hssf.usermodel.HSSFFont;
  18. import org.apache.poi.hssf.usermodel.HSSFRow;
  19. import org.apache.poi.hssf.usermodel.HSSFSheet;
  20. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  21.  
  22. /**
  23. * @描述 负责将数据集(表单数据)导出Excel文件
  24. * @author
  25.  
  26. */
  27. public class ExcelUtil {
  28.  
  29. /**
  30. * 类实例
  31. */
  32. private static ExcelUtil export;
  33.  
  34. /**
  35. * excel文档
  36. */
  37. private HSSFWorkbook workbook;
  38.  
  39. /**
  40. * excel sheet
  41. */
  42. private HSSFSheet sheet;
  43.  
  44. /**
  45. * 字节流
  46. */
  47. private OutputStream fileOutput;
  48.  
  49. /**
  50. * 声明私有构造方法
  51. */
  52. private ExcelUtil() {
  53.  
  54. }
  55.  
  56. /**
  57. * 产生一个excel导出工具类实例(单例模式)
  58. *
  59. * @return excel导出工具类对象
  60. */
  61. public static ExcelUtil newInstance() {
  62. if (export == null)
  63. export = new ExcelUtil();
  64. return export;
  65. }
  66.  
  67. /**
  68. * @功能描述 设置excel文档(单表单)
  69. * @创建人
  70. * @创建时间 2011-5-27 下午02:25:58
  71. * @param tName
  72. * excel表名集
  73. * @param tHeader
  74. * excel表头数据集
  75. * @param tValue
  76. * excel表单数据集(除表头)
  77. * @param tHeaderStyle
  78. * excel表头单元格样式
  79. * @param tValueStyle
  80. * excel表单数据单元格样式(除表头)
  81. * @param filePath
  82. * excel文件地址
  83. * @throws Exception
  84. * 异常往上抛出
  85. */
  86. public void exportExcel(String tName, ArrayList<String> tHeader, ArrayList<ArrayList<Object>> tValue,
  87. Map<String, Short> tHeaderStyle, Map<String, Short> tValueStyle, String filePath) throws Exception {
  88.  
  89. try {
  90. // 当excel文档不存在时创建
  91. workbook = new HSSFWorkbook();
  92.  
  93. // 单个表单赋值和样式
  94. this.setSheet(tName, tHeader, tValue, tHeaderStyle, tValueStyle);
  95. // 导出excel文件
  96. this.export(workbook, filePath);
  97.  
  98. } catch (Exception e) {
  99. e.printStackTrace();
  100.  
  101. }
  102. }
  103.  
  104. /**
  105. * @功能描述 设置excel文档(单表单)
  106. * @创建人
  107. * @创建时间 2011-5-27 下午02:25:58
  108. * @param tName
  109. * excel表名集
  110. * @param tHeader
  111. * excel表头数据集
  112. * @param tValue
  113. * excel表单数据集(除表头)
  114. * @param tHeaderStyle
  115. * excel表头单元格样式
  116. * @param tValueStyle
  117. * excel表单数据单元格样式(除表头)
  118. * @throws Exception
  119. * 异常往上抛出
  120. */
  121. public InputStream exportExcelToStream(String tName, ArrayList<String> tHeader,
  122. ArrayList<ArrayList<Object>> tValue, Map<String, Short> tHeaderStyle, Map<String, Short> tValueStyle)
  123. throws Exception {
  124.  
  125. // 当excel文档不存在时创建
  126. workbook = new HSSFWorkbook();
  127.  
  128. // 单个表单赋值和样式
  129. this.setSheet(tName, tHeader, tValue, tHeaderStyle, tValueStyle);
  130.  
  131. // 导出excel文件
  132. return export(workbook);
  133.  
  134. }
  135.  
  136. /**
  137. * @功能描述 设置excel文档(多表单)
  138. * @创建人
  139. * @创建时间 2011-5-27 下午02:25:58
  140. * @param tName
  141. * excel表名集
  142. * @param tHeader
  143. * excel表头数据集
  144. * @param tValue
  145. * excel表单数据集(除表头)
  146. * @param tHeaderStyle
  147. * excel表头单元格样式
  148. * @param tValueStyle
  149. * excel表单数据单元格样式(除表头)
  150. * @param filePath
  151. * excel文件地址
  152. * @throws Exception
  153. * 异常往上抛出
  154. */
  155. public void exportExcel(ArrayList<String> tName, ArrayList<ArrayList<String>> tHeader,
  156. ArrayList<ArrayList<ArrayList<Object>>> tValue, ArrayList<Map<String, Short>> tHeaderStyle,
  157. ArrayList<Map<String, Short>> tValueStyle, String filePath) throws Exception {
  158.  
  159. try {
  160. // 当excel文档不存在时创建
  161. workbook = new HSSFWorkbook();
  162.  
  163. // for循环完成文档各个表单的赋值和样式
  164. for (int i = 0; i < tName.size(); i++) {
  165. this.setSheet(tName.get(i), tHeader.get(i), tValue.get(i), tHeaderStyle.get(i), tValueStyle.get(i)); // 单个表单赋值和样式
  166. }
  167.  
  168. // 导出excel文件
  169. this.export(workbook, filePath);
  170.  
  171. } catch (Exception e) {
  172. }
  173. }
  174.  
  175. /**
  176. * @功能描述 设置excel文档(多表单)
  177. * @创建人
  178. * @创建时间 2011-5-27 下午02:25:58
  179. * @param tName
  180. * excel表名集
  181. * @param tHeader
  182. * excel表头数据集
  183. * @param tValue
  184. * excel表单数据集(除表头)
  185. * @param tHeaderStyle
  186. * excel表头单元格样式
  187. * @param tValueStyle
  188. * excel表单数据单元格样式(除表头)
  189. * @throws Exception
  190. * 异常往上抛出
  191. */
  192. public InputStream exportExcelToStream(ArrayList<String> tName, ArrayList<ArrayList<String>> tHeader,
  193. ArrayList<ArrayList<ArrayList<Object>>> tValue, ArrayList<Map<String, Short>> tHeaderStyle,
  194. ArrayList<Map<String, Short>> tValueStyle) throws Exception {
  195.  
  196. // 当excel文档不存在时创建
  197. workbook = new HSSFWorkbook();
  198.  
  199. // for循环完成文档各个表单的赋值和样式
  200. for (int i = 0; i < tName.size(); i++) {
  201. this.setSheet(tName.get(i), tHeader.get(i), tValue.get(i), tHeaderStyle.get(i), tValueStyle.get(i)); // 单个表单赋值和样式
  202. }
  203. return export(workbook);
  204. }
  205.  
  206. /**
  207. * @功能描述 设置excel表单
  208. * @创建人
  209. * @创建时间 2011-5-27 下午02:23:02
  210. * @param tName
  211. * excel表名
  212. * @param tHeader
  213. * excel表头数据集
  214. * @param tValue
  215. * excel表单数据集(除表头)
  216. * @param tHeaderStyle
  217. * excel表头单元格样式
  218. * @param tValueStyle
  219. * excel表单数据单元格样式(除表头)
  220. * @throws Exception
  221. * 异常往上抛出
  222. */
  223. private void setSheet(String tName, ArrayList<String> tHeader, ArrayList<ArrayList<Object>> tValue,
  224. Map<String, Short> tHeaderStyle, Map<String, Short> tValueStyle) throws Exception {
  225.  
  226. try {
  227. // 创建表单并设置其表名
  228. sheet = workbook.createSheet(tName);
  229.  
  230. // 创建表单行
  231. HSSFRow tRow = sheet.createRow(0);
  232.  
  233. // 赋值和样式(此时,为表头行)
  234. tRow = this.setTRow(tRow, tHeader, tHeaderStyle);
  235.  
  236. // for循环完成表单数据的赋值和样式(除表头)
  237. for (int i = 0; i < tValue.size(); i++) {
  238. tRow = sheet.createRow(i + 1); // 获取表单行
  239.  
  240. tRow = this.setTRow(tRow, tValue.get(i), tValueStyle); // 设置当前行的数据和样式
  241. }
  242. } catch (Exception e) {
  243. e.printStackTrace();
  244. }
  245. }
  246.  
  247. /**
  248. * @功能描述 设置excel表单行数据
  249. * @创建人
  250. * @创建时间 2011-5-27 下午02:22:30
  251. * @param row
  252. * excel表单行
  253. * @param tRow
  254. * excel表单行数据
  255. * @param tHeaderStyle
  256. * excel表头样式
  257. * @return 设置后的的表单行
  258. * @throws Exception
  259. * 异常往外抛出
  260. */
  261. @SuppressWarnings("unchecked")
  262. private HSSFRow setTRow(HSSFRow row, ArrayList tRow, Map<String, Short> tHeaderStyle) throws Exception {
  263.  
  264. try {
  265. // 获取单元格样式
  266. HSSFCellStyle cellStyle = this.setCellStyle(tHeaderStyle);
  267. // 声明单元格
  268. HSSFCell cell = null;
  269.  
  270. // for循环完成该表单某行各个列赋值和样式
  271. for (int i = 0; i < tRow.size(); i++) {
  272. cell = row.createCell(i); // 获取每列单元格
  273. cell.setCellStyle(cellStyle); // 设置样式
  274.  
  275. sheet.autoSizeColumn((short) i); // 设置单元格自适应
  276. Object obj = tRow.get(i); // 获取当前列的值
  277. // 判断对象所属类型, 并强转
  278. if (obj instanceof Integer) // 当数字时
  279. cell.setCellValue((Integer) obj);
  280. if (obj instanceof String) // 当为字符串时
  281. cell.setCellValue((String) obj);
  282. if (obj instanceof Boolean) // 当为布尔时
  283. cell.setCellValue((Boolean) obj);
  284. if (obj instanceof Date) // 当为时间时
  285. cell.setCellValue((Date) obj);
  286. if (obj instanceof Calendar) // 当为时间时
  287. cell.setCellValue((Calendar) obj);
  288. if (obj instanceof Double) // 当为小数时
  289. cell.setCellValue((Double) obj);
  290. }
  291. } catch (Exception e) {
  292. e.printStackTrace();
  293. }
  294. return row; // 返回
  295. }
  296.  
  297. /**
  298. * @功能描述 设置单元格样式
  299. * @创建人
  300. * @创建时间 2011-5-27 下午02:21:40
  301. * @param fontStyle
  302. * 样式Map集合
  303. * @return 设置后单元格样式
  304. * @throws Exception
  305. * 异常往外抛出
  306. */
  307. private HSSFCellStyle setCellStyle(Map<String, Short> fontStyle) throws Exception {
  308.  
  309. // 声明单元格样式
  310. HSSFCellStyle cellStyle = null;
  311. try {
  312. // 创建字体
  313. HSSFFont font = workbook.createFont();
  314. // 设置字体样式
  315. // 设置字体颜色(红色为:HSSFFont.COLOR_RED 这里表示short类型 10)
  316. font.setColor(fontStyle.get("color"));
  317. // 设置字体形体(宽体为:HSSFFont.BOLDWEIGHT_BOLD 700) -- 粗体
  318. font.setBoldweight(fontStyle.get("weight"));
  319.  
  320. // 创建单元格样式
  321. cellStyle = workbook.createCellStyle();
  322. // 添加字体样式
  323. cellStyle.setFont(font);
  324.  
  325. } catch (Exception e) {
  326. e.printStackTrace();
  327. }
  328. return cellStyle; // 返回
  329. }
  330.  
  331. /**
  332. * @功能描述 导出Excel
  333. * @创建人
  334. * @创建时间 2011-5-27 下午02:57:37
  335. * @param workbook
  336. * excel文档
  337. * @param filePath
  338. * xls文件地址
  339. * @throws Exception
  340. * 异常往外抛出
  341. */
  342. private void export(HSSFWorkbook workbook, String filePath) throws Exception {
  343.  
  344. try {
  345. // 根据指定xls文件创建文件字符流
  346. fileOutput = new FileOutputStream(filePath);
  347. // 将文档写入指定文件
  348. workbook.write(fileOutput);
  349.  
  350. } catch (FileNotFoundException e) {
  351. e.printStackTrace();
  352. } catch (Exception e) {
  353. e.printStackTrace();
  354. } finally {
  355. try {
  356. // 关闭流, 释放资源
  357. fileOutput.close();
  358. } catch (IOException e) {
  359. e.printStackTrace();
  360. ;
  361. }
  362. }
  363. }
  364.  
  365. /**
  366. * @功能描述 获取流
  367. * @创建人
  368. * @创建时间 2011-5-27 下午02:57:37
  369. * @param workbook
  370. * excel文档
  371. * @throws Exception
  372. * 异常往外抛出
  373. */
  374. private InputStream export(HSSFWorkbook workbook) throws IOException {
  375.  
  376. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  377. try {
  378. try {
  379. workbook.write(baos);
  380. } catch (IOException e) {
  381. e.printStackTrace();
  382. }
  383. byte[] ba = baos.toByteArray();
  384. ByteArrayInputStream bais = new ByteArrayInputStream(ba);
  385. return bais;
  386. } finally {
  387. // 关闭流, 释放资源
  388. baos.close();
  389. }
  390. }
  391. }
  1.  
  2. package com.zuidaima.excel.util;
  3.  
  4. import java.io.ByteArrayInputStream;
  5. import java.io.ByteArrayOutputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.OutputStream;
  11. import java.util.ArrayList;
  12. import java.util.Calendar;
  13. import java.util.Date;
  14. import java.util.Map;
  15.  
  16. import org.apache.poi.hssf.usermodel.HSSFCell;
  17. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  18. import org.apache.poi.hssf.usermodel.HSSFFont;
  19. import org.apache.poi.hssf.usermodel.HSSFRow;
  20. import org.apache.poi.hssf.usermodel.HSSFSheet;
  21. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  22.  
  23. /**
  24. * @描述 负责将数据集(表单数据)导出Excel文件
  25.  
  26. */
  27. public class ExcelUtil {
  28.  
  29. /**
  30. * 类实例
  31. */
  32. private static ExcelUtil export;
  33.  
  34. /**
  35. * excel文档
  36. */
  37. private HSSFWorkbook workbook;
  38.  
  39. /**
  40. * excel sheet
  41. */
  42. private HSSFSheet sheet;
  43.  
  44. /**
  45. * 字节流
  46. */
  47. private OutputStream fileOutput;
  48.  
  49. /**
  50. * 声明私有构造方法
  51. */
  52. private ExcelUtil() {
  53.  
  54. }
  55.  
  56. /**
  57. * 产生一个excel导出工具类实例(单例模式)
  58. *
  59. * @return excel导出工具类对象
  60. */
  61. public static ExcelUtil newInstance() {
  62. if (export == null)
  63. export = new ExcelUtil();
  64. return export;
  65. }
  66.  
  67. /**
  68. * @功能描述 设置excel文档(单表单)
  69. * @创建人
  70. * @创建时间 2011-5-27 下午02:25:58
  71. * @param tName
  72. * excel表名集
  73. * @param tHeader
  74. * excel表头数据集
  75. * @param tValue
  76. * excel表单数据集(除表头)
  77. * @param tHeaderStyle
  78. * excel表头单元格样式
  79. * @param tValueStyle
  80. * excel表单数据单元格样式(除表头)
  81. * @param filePath
  82. * excel文件地址
  83. * @throws Exception
  84. * 异常往上抛出
  85. */
  86. public void exportExcel(String tName, ArrayList<String> tHeader, ArrayList<ArrayList<Object>> tValue,
  87. Map<String, Short> tHeaderStyle, Map<String, Short> tValueStyle, String filePath) throws Exception {
  88.  
  89. try {
  90. // 当excel文档不存在时创建
  91. workbook = new HSSFWorkbook();
  92.  
  93. // 单个表单赋值和样式
  94. this.setSheet(tName, tHeader, tValue, tHeaderStyle, tValueStyle);
  95. // 导出excel文件
  96. this.export(workbook, filePath);
  97.  
  98. } catch (Exception e) {
  99. e.printStackTrace();
  100.  
  101. }
  102. }
  103.  
  104. /**
  105. * @功能描述 设置excel文档(单表单)
  106. * @创建人
  107. * @创建时间 2011-5-27 下午02:25:58
  108. * @param tName
  109. * excel表名集
  110. * @param tHeader
  111. * excel表头数据集
  112. * @param tValue
  113. * excel表单数据集(除表头)
  114. * @param tHeaderStyle
  115. * excel表头单元格样式
  116. * @param tValueStyle
  117. * excel表单数据单元格样式(除表头)
  118. * @throws Exception
  119. * 异常往上抛出
  120. */
  121. public InputStream exportExcelToStream(String tName, ArrayList<String> tHeader,
  122. ArrayList<ArrayList<Object>> tValue, Map<String, Short> tHeaderStyle, Map<String, Short> tValueStyle)
  123. throws Exception {
  124.  
  125. // 当excel文档不存在时创建
  126. workbook = new HSSFWorkbook();
  127.  
  128. // 单个表单赋值和样式
  129. this.setSheet(tName, tHeader, tValue, tHeaderStyle, tValueStyle);
  130.  
  131. // 导出excel文件
  132. return export(workbook);
  133.  
  134. }
  135.  
  136. /**
  137. * @功能描述 设置excel文档(多表单)
  138. * @创建人
  139. * @创建时间 2011-5-27 下午02:25:58
  140. * @param tName
  141. * excel表名集
  142. * @param tHeader
  143. * excel表头数据集
  144. * @param tValue
  145. * excel表单数据集(除表头)
  146. * @param tHeaderStyle
  147. * excel表头单元格样式
  148. * @param tValueStyle
  149. * excel表单数据单元格样式(除表头)
  150. * @param filePath
  151. * excel文件地址
  152. * @throws Exception
  153. * 异常往上抛出
  154. */
  155. public void exportExcel(ArrayList<String> tName, ArrayList<ArrayList<String>> tHeader,
  156. ArrayList<ArrayList<ArrayList<Object>>> tValue, ArrayList<Map<String, Short>> tHeaderStyle,
  157. ArrayList<Map<String, Short>> tValueStyle, String filePath) throws Exception {
  158.  
  159. try {
  160. // 当excel文档不存在时创建
  161. workbook = new HSSFWorkbook();
  162.  
  163. // for循环完成文档各个表单的赋值和样式
  164. for (int i = 0; i < tName.size(); i++) {
  165. this.setSheet(tName.get(i), tHeader.get(i), tValue.get(i), tHeaderStyle.get(i), tValueStyle.get(i)); // 单个表单赋值和样式
  166. }
  167.  
  168. // 导出excel文件
  169. this.export(workbook, filePath);
  170.  
  171. } catch (Exception e) {
  172. }
  173. }
  174.  
  175. /**
  176. * @功能描述 设置excel文档(多表单)
  177. * @创建人
  178. * @创建时间 2011-5-27 下午02:25:58
  179. * @param tName
  180. * excel表名集
  181. * @param tHeader
  182. * excel表头数据集
  183. * @param tValue
  184. * excel表单数据集(除表头)
  185. * @param tHeaderStyle
  186. * excel表头单元格样式
  187. * @param tValueStyle
  188. * excel表单数据单元格样式(除表头)
  189. * @throws Exception
  190. * 异常往上抛出
  191. */
  192. public InputStream exportExcelToStream(ArrayList<String> tName, ArrayList<ArrayList<String>> tHeader,
  193. ArrayList<ArrayList<ArrayList<Object>>> tValue, ArrayList<Map<String, Short>> tHeaderStyle,
  194. ArrayList<Map<String, Short>> tValueStyle) throws Exception {
  195.  
  196. // 当excel文档不存在时创建
  197. workbook = new HSSFWorkbook();
  198.  
  199. // for循环完成文档各个表单的赋值和样式
  200. for (int i = 0; i < tName.size(); i++) {
  201. this.setSheet(tName.get(i), tHeader.get(i), tValue.get(i), tHeaderStyle.get(i), tValueStyle.get(i)); // 单个表单赋值和样式
  202. }
  203. return export(workbook);
  204. }
  205.  
  206. /**
  207. * @功能描述 设置excel表单
  208. * @创建人
  209. * @创建时间 2011-5-27 下午02:23:02
  210. * @param tName
  211. * excel表名
  212. * @param tHeader
  213. * excel表头数据集
  214. * @param tValue
  215. * excel表单数据集(除表头)
  216. * @param tHeaderStyle
  217. * excel表头单元格样式
  218. * @param tValueStyle
  219. * excel表单数据单元格样式(除表头)
  220. * @throws Exception
  221. * 异常往上抛出
  222. */
  223. private void setSheet(String tName, ArrayList<String> tHeader, ArrayList<ArrayList<Object>> tValue,
  224. Map<String, Short> tHeaderStyle, Map<String, Short> tValueStyle) throws Exception {
  225.  
  226. try {
  227. // 创建表单并设置其表名
  228. sheet = workbook.createSheet(tName);
  229.  
  230. // 创建表单行
  231. HSSFRow tRow = sheet.createRow(0);
  232.  
  233. // 赋值和样式(此时,为表头行)
  234. tRow = this.setTRow(tRow, tHeader, tHeaderStyle);
  235.  
  236. // for循环完成表单数据的赋值和样式(除表头)
  237. for (int i = 0; i < tValue.size(); i++) {
  238. tRow = sheet.createRow(i + 1); // 获取表单行
  239.  
  240. tRow = this.setTRow(tRow, tValue.get(i), tValueStyle); // 设置当前行的数据和样式
  241. }
  242. } catch (Exception e) {
  243. e.printStackTrace();
  244. }
  245. }
  246.  
  247. /**
  248. * @功能描述 设置excel表单行数据
  249. * @创建人
  250. * @创建时间 2011-5-27 下午02:22:30
  251. * @param row
  252. * excel表单行
  253. * @param tRow
  254. * excel表单行数据
  255. * @param tHeaderStyle
  256. * excel表头样式
  257. * @return 设置后的的表单行
  258. * @throws Exception
  259. * 异常往外抛出
  260. */
  261. @SuppressWarnings("unchecked")
  262. private HSSFRow setTRow(HSSFRow row, ArrayList tRow, Map<String, Short> tHeaderStyle) throws Exception {
  263.  
  264. try {
  265. // 获取单元格样式
  266. HSSFCellStyle cellStyle = this.setCellStyle(tHeaderStyle);
  267. // 声明单元格
  268. HSSFCell cell = null;
  269.  
  270. // for循环完成该表单某行各个列赋值和样式
  271. for (int i = 0; i < tRow.size(); i++) {
  272. cell = row.createCell(i); // 获取每列单元格
  273. cell.setCellStyle(cellStyle); // 设置样式
  274.  
  275. sheet.autoSizeColumn((short) i); // 设置单元格自适应
  276. Object obj = tRow.get(i); // 获取当前列的值
  277. // 判断对象所属类型, 并强转
  278. if (obj instanceof Integer) // 当数字时
  279. cell.setCellValue((Integer) obj);
  280. if (obj instanceof String) // 当为字符串时
  281. cell.setCellValue((String) obj);
  282. if (obj instanceof Boolean) // 当为布尔时
  283. cell.setCellValue((Boolean) obj);
  284. if (obj instanceof Date) // 当为时间时
  285. cell.setCellValue((Date) obj);
  286. if (obj instanceof Calendar) // 当为时间时
  287. cell.setCellValue((Calendar) obj);
  288. if (obj instanceof Double) // 当为小数时
  289. cell.setCellValue((Double) obj);
  290. }
  291. } catch (Exception e) {
  292. e.printStackTrace();
  293. }
  294. return row; // 返回
  295. }
  296.  
  297. /**
  298. * @功能描述 设置单元格样式
  299. * @创建人
  300. * @创建时间 2011-5-27 下午02:21:40
  301. * @param fontStyle
  302. * 样式Map集合
  303. * @return 设置后单元格样式
  304. * @throws Exception
  305. * 异常往外抛出
  306. */
  307. private HSSFCellStyle setCellStyle(Map<String, Short> fontStyle) throws Exception {
  308.  
  309. // 声明单元格样式
  310. HSSFCellStyle cellStyle = null;
  311. try {
  312. // 创建字体
  313. HSSFFont font = workbook.createFont();
  314. // 设置字体样式
  315. // 设置字体颜色(红色为:HSSFFont.COLOR_RED 这里表示short类型 10)
  316. font.setColor(fontStyle.get("color"));
  317. // 设置字体形体(宽体为:HSSFFont.BOLDWEIGHT_BOLD 700) -- 粗体
  318. font.setBoldweight(fontStyle.get("weight"));
  319.  
  320. // 创建单元格样式
  321. cellStyle = workbook.createCellStyle();
  322. // 添加字体样式
  323. cellStyle.setFont(font);
  324.  
  325. } catch (Exception e) {
  326. e.printStackTrace();
  327. }
  328. return cellStyle; // 返回
  329. }
  330.  
  331. /**
  332. * @功能描述 导出Excel
  333. * @创建人
  334. * @创建时间 2011-5-27 下午02:57:37
  335. * @param workbook
  336. * excel文档
  337. * @param filePath
  338. * xls文件地址
  339. * @throws Exception
  340. * 异常往外抛出
  341. */
  342. private void export(HSSFWorkbook workbook, String filePath) throws Exception {
  343.  
  344. try {
  345. // 根据指定xls文件创建文件字符流
  346. fileOutput = new FileOutputStream(filePath);
  347. // 将文档写入指定文件
  348. workbook.write(fileOutput);
  349.  
  350. } catch (FileNotFoundException e) {
  351. e.printStackTrace();
  352. } catch (Exception e) {
  353. e.printStackTrace();
  354. } finally {
  355. try {
  356. // 关闭流, 释放资源
  357. fileOutput.close();
  358. } catch (IOException e) {
  359. e.printStackTrace();
  360. ;
  361. }
  362. }
  363. }
  364.  
  365. /**
  366. * @功能描述 获取流
  367. * @创建人
  368. * @创建时间 2011-5-27 下午02:57:37
  369. * @param workbook
  370. * excel文档
  371. * @throws Exception
  372. * 异常往外抛出
  373. */
  374. private InputStream export(HSSFWorkbook workbook) throws IOException {
  375.  
  376. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  377. try {
  378. try {
  379. workbook.write(baos);
  380. } catch (IOException e) {
  381. e.printStackTrace();
  382. }
  383. byte[] ba = baos.toByteArray();
  384. ByteArrayInputStream bais = new ByteArrayInputStream(ba);
  385. return bais;
  386. } finally {
  387. // 关闭流, 释放资源
  388. baos.close();
  389. }
  390. }
  391. }

java 通过Apache poi导出excel代码demo实例的更多相关文章

  1. 使用Apache POI导出Excel小结--导出XLS格式文档

    使用Apache POI导出Excel小结 关于使用Apache POI导出Excel我大概会分三篇文章去写 使用Apache POI导出Excel小结--导出XLS格式文档 使用Apache POI ...

  2. java中使用poi导出excel表格数据并且可以手动修改导出路径

    在我们开发项目中,很多时候会提出这样的需求:将前端的某某数据以excel表格导出,今天就给大家写一个简单的模板. 这里我们选择使用poi导出excel: 第一步:导入需要的jar包到 lib 文件夹下

  3. apache POI 导出excel相关方法

    apache POI 操作excel无比强大.同时有操作word和ppt的接口. 下面讲解poi中常用方法. 1,设置列宽 HSSFSheet sheet = wb.getSheetAt(0); sh ...

  4. Java使用Apache POI进行Excel导入和导出

    Manve依赖 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> ...

  5. Java中用Apache POI生成excel和word文档

    概述: 近期在做项目的过程中遇到了excel的数据导出和word的图文表报告的导出功能.最后决定用Apache POI来完毕该项功能.本文就项目实现过程中的一些思路与代码与大家共享.同一时候.也作为自 ...

  6. Apache POI导出excel表格

    项目中我们经常用到导出功能,将数据导出以便于审查和统计等.本文主要使用Apache POI实现导出数据. POI中文文档 简介 ApachePOI是Apache软件基金会的开放源码函式库,POI提供A ...

  7. apache poi导出excel报表

    Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能.POI为"P ...

  8. java使用Apache POI操作excel文件

    官方介绍 HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is ...

  9. 使用org.apache.poi导出Excel表格

    public HSSFWorkbook MakeExcel(List<TransactionLogVO> logList) { // SimpleDateFormat sdf = new ...

随机推荐

  1. Inno Setup 注册表启动项 修改注册表

      //注册表启动项 [Registry] Root: HKLM; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; ...

  2. jquery获取css颜色值返回RGB应用

    我来给大家介绍一下在jquery获取css颜色值返回RGB方法,希望此文章对各位同学会有所帮助哦.   代码如下:  代码如下 复制代码 a, a:link, a:visited { color:#4 ...

  3. CSS部分属性的深入学习

    先上个张鑫旭大神的政治课,来个一棒打醒(手动滑稽): 说说CSS学习中的瓶颈: 虽然自己水平不高,但是对于重构这方面工作一直不怎么喜欢,可能觉得比较没有新意,但是看了大神文章突然有点一棍打醒的感觉,突 ...

  4. easyui textbox获取焦点事件

    $('#textboxid').textbox().next('span').find('input').focus(); $('#id').textbox('textbox').focus();

  5. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:3.安装Oracle RAC-3.4.安装Grid Infrastructure

    3.4.安装Grid Infrastructure 3.4.1.安装Grid 1.运行 grid的安装文件runInstaller [grid@linuxrac1 grid]$ ./runInstal ...

  6. phpmyadmin后台4种拿shell方法 && php爆路径大法

    php后台拿shell要知道php的路径,文章下面将讲诉爆php路径的方法!!! 方法一: CREATE TABLE `mysql`.`xss` (`xss1` TEXT NOT NULL ); IN ...

  7. 踩坑记:httpComponents 的 EntityUtils

    今天写的一个服务程序,有人报告获得的数据中文乱码,而我是用 apache 通过 httpComponents 去取得数据的.于是开启日志的 debug 级别. 在日志里果然发现中文不见了,有乱码出现: ...

  8. Java基础——Statement与PrepareStatement

    Statement Statement是Java运行数据库操作的一个重要方法.用于在已经建立数据库连接的基础上.向数据库发送要运行的SQL语句.Statement对象,用于运行不带參数的简单SQL语句 ...

  9. Joomla详细安装图文教程

    Joomla 详细安装图文教程 第一步,配置网站信息 配置数据库:这里我选择MySQLi,可以根据自己的选择         安装-- 安装完成!

  10. jQuery 创建html

    jQuery 创建html