首页 文章 精选 留言 我的

精选列表

搜索[maven],共5963篇文章
优秀的个人博客,低调大师

Springmvc导出Excel(maven)

一、导入依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.9</version> </dependency> 二、编写工具类 package cn.pms.util; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; import java.text.SimpleDateFormat; import java.util.Date; import java.util.LinkedHashMap; import java.util.List; import java.util.Map.Entry; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 导出Excel * @author Guang * */ public class ExportExcelUtils { private static final Logger logger =LoggerFactory.getLogger(ExportExcelUtils.class); /** * 导出Excel * @param excelName 要导出的excel名称 * @param list 要导出的数据集合 * @param fieldMap 中英文字段对应Map,即要导出的excel表头 * @param response 使用response可以导出到浏览器 * @return */ public static <T> void export(String excelName,List<T> list,LinkedHashMap<String, String> fieldMap,HttpServletResponse response){ // 设置默认文件名为当前时间:年月日时分秒 if (excelName==null || excelName=="") { excelName = new SimpleDateFormat("yyyyMMddhhmmss").format( new Date()).toString(); } // 设置response头信息 response.reset(); response.setContentType("application/vnd.ms-excel"); // 改成输出excel文件 try { response.setHeader("Content-disposition", "attachment; filename=" +new String(excelName.getBytes("gb2312"), "ISO-8859-1") + ".xls"); } catch (UnsupportedEncodingException e1) { logger.info(e1.getMessage()); } try { //创建一个WorkBook,对应一个Excel文件 HSSFWorkbook wb=new HSSFWorkbook(); //在Workbook中,创建一个sheet,对应Excel中的工作薄(sheet) HSSFSheet sheet=wb.createSheet(excelName); //创建单元格,并设置值表头 设置表头居中 HSSFCellStyle style=wb.createCellStyle(); //创建一个居中格式 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 填充工作表 fillSheet(sheet,list,fieldMap,style); //将文件输出 OutputStream ouputStream = response.getOutputStream(); wb.write(ouputStream); ouputStream.flush(); ouputStream.close(); } catch (Exception e) { logger.info("导出Excel失败!"); logger.error(e.getMessage()); } } /** * 根据字段名获取字段对象 * * @param fieldName * 字段名 * @param clazz * 包含该字段的类 * @return 字段 */ public static Field getFieldByName(String fieldName, Class<?> clazz) { logger.info("根据字段名获取字段对象:getFieldByName()"); // 拿到本类的所有字段 Field[] selfFields = clazz.getDeclaredFields(); // 如果本类中存在该字段,则返回 for (Field field : selfFields) { //如果本类中存在该字段,则返回 if (field.getName().equals(fieldName)) { return field; } } // 否则,查看父类中是否存在此字段,如果有则返回 Class<?> superClazz = clazz.getSuperclass(); if (superClazz != null && superClazz != Object.class) { //递归 return getFieldByName(fieldName, superClazz); } // 如果本类和父类都没有,则返回空 return null; } /** * 根据字段名获取字段值 * * @param fieldName 字段名 * @param o 对象 * @return 字段值 * @throws Exception 异常 * */ public static Object getFieldValueByName(String fieldName, Object o) throws Exception { logger.info("根据字段名获取字段值:getFieldValueByName()"); Object value = null; //根据字段名得到字段对象 Field field = getFieldByName(fieldName, o.getClass()); //如果该字段存在,则取出该字段的值 if (field != null) { field.setAccessible(true);//类中的成员变量为private,在类外边使用属性值,故必须进行此操作 value = field.get(o);//获取当前对象中当前Field的value } else { throw new Exception(o.getClass().getSimpleName() + "类不存在字段名 " + fieldName); } return value; } /** * 根据带路径或不带路径的属性名获取属性值,即接受简单属性名, * 如userName等,又接受带路径的属性名,如student.department.name等 * * @param fieldNameSequence 带路径的属性名或简单属性名 * @param o 对象 * @return 属性值 * @throws Exception 异常 * */ public static Object getFieldValueByNameSequence(String fieldNameSequence, Object o) throws Exception { logger.info("根据带路径或不带路径的属性名获取属性值,即接受简单属性名:getFieldValueByNameSequence()"); Object value = null; // 将fieldNameSequence进行拆分 String[] attributes = fieldNameSequence.split("\\."); if (attributes.length == 1) { value = getFieldValueByName(fieldNameSequence, o); } else { // 根据数组中第一个连接属性名获取连接属性对象,如student.department.name Object fieldObj = getFieldValueByName(attributes[0], o); //截取除第一个属性名之后的路径 String subFieldNameSequence = fieldNameSequence .substring(fieldNameSequence.indexOf(".") + 1); //递归得到最终的属性对象的值 value = getFieldValueByNameSequence(subFieldNameSequence, fieldObj); } return value; } /** * 向工作表中填充数据 * * @param sheet * excel的工作表名称 * @param list * 数据源 * @param fieldMap * 中英文字段对应关系的Map * @param style * 表格中的格式 * @throws Exception * 异常 * */ public static <T> void fillSheet(HSSFSheet sheet, List<T> list, LinkedHashMap<String, String> fieldMap,HSSFCellStyle style) throws Exception { logger.info("向工作表中填充数据:fillSheet()"); // 定义存放英文字段名和中文字段名的数组 String[] enFields = new String[fieldMap.size()]; String[] cnFields = new String[fieldMap.size()]; // 填充数组 int count = 0; for (Entry<String, String> entry : fieldMap.entrySet()) { enFields[count] = entry.getKey(); cnFields[count] = entry.getValue(); count++; } //在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short HSSFRow row=sheet.createRow((int)0); // 填充表头 for (int i = 0; i < cnFields.length; i++) { HSSFCell cell=row.createCell(i); cell.setCellValue(cnFields[i]); cell.setCellStyle(style); sheet.autoSizeColumn(i); } // 填充内容 for (int index = 0; index < list.size(); index++) { row = sheet.createRow(index + 1); // 获取单个对象 T item = list.get(index); for (int i = 0; i < enFields.length; i++) { Object objValue = getFieldValueByNameSequence(enFields[i], item); String fieldValue = objValue == null ? "" : objValue.toString(); row.createCell(i).setCellValue(fieldValue); } } } } 三、编写Controller package cn.pms.controller; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import com.alibaba.fastjson.JSON; import com.wordnik.swagger.annotations.Api; import com.wordnik.swagger.annotations.ApiOperation; import cn.pms.model.User; import cn.pms.service.UserService; import cn.pms.util.ExcelImportUtil; import cn.pms.util.ExportExcelUtils; /** * 用户Controller * * @author eluzhu * */ @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping(value = "/excel/exportBankCheckInfo", method = RequestMethod.GET) public void ExportBankCheckInfo(HttpServletRequest request, HttpServletResponse response) { //得到所要导出的数据 List<User> errLst = userService.selectAllUser(); //定义导出excel的名字 String excelName="用户表"; // 获取需要转出的excle表头的map字段 LinkedHashMap<String, String> fieldMap =new LinkedHashMap<String, String>() ; fieldMap.put("userNo", "编号"); fieldMap.put("userName", "姓名"); fieldMap.put("password", "密码"); //导出用户相关信息 new ExportExcelUtils().export(excelName, errLst, fieldMap, response); } } 四、浏览器输入对应的URL进行测试(成功的标识如下) 控制台打印和浏览器显示

优秀的个人博客,低调大师

Maven hive-jdbc教程

由于项目中需要用到hive-jdbc从数据仓库拉数据下来,所以简单的学一下hive,hive数据仓库建构在hadoop集群之上,数据存在hdfs文件系统中,hive中执行的操作会装换成mapreduce作业进行执行,hive支持类似SQL的语言HQL,hive采用元数据对表进行管理,元数据有三种存放模式:嵌入模式,远程模式,本地模式;hive提供了强大的编程接口,hive jdbc可以让你如使用普通的jdbc一般来操作hive表以及数据。 1.添加依赖 <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.6.4</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.6.4</version> </dependency> <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-exec</artifactId> <version>1.1.0</version> <exclusions> <exclusion> <artifactId> pentaho-aggdesigner-algorithm </artifactId> <groupId>org.pentaho</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-jdbc</artifactId> <version>1.1.0</version> </dependency> 2.jdbc连接hive public class TestHive { private static String driverName = "org.apache.hive.jdbc.HiveDriver";//jdbc驱动路径 private static String url = "jdbc:hive2://hiveserver.xxx.com:10000/dbName";//hive库地址+库名 private static String user = "username";//用户名 private static String password = "pwd";//密码 private static String sql = ""; private static ResultSet res; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { conn = getConn(); System.out.println(conn); stmt = conn.createStatement(); String tableName="tab_name";//hive表名 sql = "select * from " + tableName; System.out.println("Running:" + sql); res = stmt.executeQuery(sql); System.out.println("执行 select * query 运行结果:"); while (res.next()) { System.out.println(res.getInt(1) + "\t" + res.getString(2)); } } catch (ClassNotFoundException e) { e.printStackTrace(); System.exit(1); } catch (SQLException e) { e.printStackTrace(); System.exit(1); } finally { try { if (conn != null) { conn.close(); conn = null; } if (stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e) { e.printStackTrace(); } } } private static Connection getConn() throws ClassNotFoundException, SQLException { Class.forName(driverName); Connection conn = DriverManager.getConnection(url, user, password); return conn; } } 3.查询hive表数据 这个就和普通的jdbc差不太多,也是用sql的方式进行查询,具体的查询语法,可以参考hive官网 4.封装hive工具类 写完了再贴上来 @落雨 ae6623.cn

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

用户登录
用户注册