您的位置:首页 > 其它

每日记载内容总结39

2014-11-28 14:21 267 查看
Apache POI

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

HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF - 提供读写Microsoft Word DOC格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读Microsoft Visio格式档案的功能。
HPBF - 提供读Microsoft Publisher格式档案的功能。
HSMF - 提供读Microsoft Outlook格式档案的功能。

String filePath = "D://test.xls";
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("数据记录");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
HSSFRow row2 = sheet.createRow((int) 1);
//设置表头字体样式
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 11);
font.setFontName("宋体");
font.setColor(HSSFColor.BLACK.index);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

//设置内容字体样式
HSSFFont font2 = wb.createFont();
font2.setFontHeightInPoints((short) 11);
font2.setFontName("宋体");
font2.setColor(HSSFColor.BLACK.index);
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 第四步,创建单元格样式,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个水平居中格式
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//创建一个竖直居中格式
style.setFont(font);
HSSFCellStyle style2 = wb.createCellStyle();
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style2.setFont(font2);
HSSFCell cell = row.createCell(0);
HSSFCell cell2 = row2.createCell(0);

cell.setCellValue("查询时间");
cell.setCellStyle(style);
cell2.setCellValue("");
cell2.setCellStyle(style);

cell = row.createCell(1);
cell.setCellValue("自助查询阶段");
cell.setCellStyle(style);
cell2 = row2.createCell(1);
cell2.setCellValue("产品类型");
cell2.setCellStyle(style);

cell = row.createCell(2);
cell.setCellValue("自助查询阶段");
cell.setCellStyle(style);
cell2 = row2.createCell(2);
cell2.setCellValue("产品型号");
cell2.setCellStyle(style);

cell = row.createCell(3);
cell.setCellValue("最后阶段");
cell.setCellStyle(style);
cell2 = row2.createCell(3);
cell2.setCellValue("产品类型");
cell2.setCellStyle(style);

for (int i = 1; i < 4; i++)
{
row = sheet.createRow((int) i+1);
row.createCell(0).setCellStyle(style2);
row.createCell(0).setCellValue("测试");
row.createCell(1).setCellValue("测试");
row.createCell(2).setCellValue("测试");
row.createCell(3).setCellValue("测试");
}
// 第六步,如果表格有需要,可以将单元格合并 行开始位置 行结束位置 列开始位置 列结束位置
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 2));
try
{
FileOutputStream fout = new FileOutputStream(filePath);
wb.write(fout);
fout.close();
}
catch (Exception e){
e.printStackTrace();
}


效果如下:



2.通过ResultSet获取数值时注意事项:

(1)获取数据库里面存储的时间的话,rs.getDate("addTime") 只能获取日期,rs.getTimestamp("addTime")可以获取日期以及时间

(2)数据库字段类型为int,如果数据库内该值是空的话,rs.getInt("faqisok")获取到的值为0,需要修改为rs.getObject("faqisok") == null ? null : rs.getInt("faqisok")

3.request.getServletContext() getRealPath("/") request.getContextPath()三者关系:
(1)request.getServletContext() 获取的是Servlet容器对象,相当于tomcat容器了。

(2)request.getServletContext().getRealPath("/") 获取实际路径,“/”指代项目根目录,所以代码返回的是项目在容器中的实际发布运行的根路径如:

I:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\UMPWeb_20131230\
(3)request.getContextPath()应该是得到项目的名字,如果项目为根目录,则得到一个"",即空的字条串,

因此,获取项目链接可以通过以下方式:

public String getHostUrl(HttpServletRequest request) {
String hostName=request.getServerName();
Integer hostPort=request.getServerPort();
String path = request.getContextPath();

if(hostPort==80) {
return "http://"+hostName+path+"/";
} else {
return "http://"+hostName+":"+hostPort+path+"/";
}
}


4.在不存在的文件夹内创建文件:

两种方式:

方式1(错误,此方法创建的是text.txt文件夹,将来写入内容的时候会保存)

String filePath = "D://testfolder//test.txt";
if(!new File(filePath).exists()){
new File(filePath).mkdirs();
}


方式2(正确,先创建父文件夹,然后再创建文件,最后写入内容)

String filePath = "D://testfolder";
if(!new File(filePath).exists()){
new File(filePath).mkdirs();
}
String file = filePath+"//test.txt";
if(!new File(file).exists()){
try {
new File(file).createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: