org.apache.poi对更高excel文档的操作
1.导入jar包
dom4j-20040902.021138.jar
ooxml-schemas-1.1.jar
poi-3.9.jar
poi-ooxml-3.9.jar
xmlbeans-2.5.0.jar
2.案例代码
package xls;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* @description 操作excel2007
* @url http://blog.csdn.net/howareyoutodaysoft
* @qq 2481151614
* @author YHZ
* @date 2013-1-14
*
*/
public class XlsxUtil {
/**
* 写入
* @param path
*/
public void writeExcel2007(String path){
XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
XSSFSheet sheet = wb.createSheet("Fonts");
Font font0 = wb.createFont();
font0.setColor(IndexedColors.BROWN.getIndex());
CellStyle style0 = wb.createCellStyle();
style0.setFont(font0);
Font font1 = wb.createFont();
font1.setFontHeightInPoints((short)14);
font1.setFontName("Courier New");
font1.setColor(IndexedColors.RED.getIndex());
CellStyle style1 = wb.createCellStyle();
style1.setFont(font1);
Font font2 = wb.createFont();
font2.setFontHeightInPoints((short)16);
font2.setFontName("Arial");
font2.setColor(IndexedColors.GREEN.getIndex());
CellStyle style2 = wb.createCellStyle();
style2.setFont(font2);
Font font3 = wb.createFont();
font3.setFontHeightInPoints((short)18);
font3.setFontName("Times New Roman");
font3.setColor(IndexedColors.LAVENDER.getIndex());
CellStyle style3 = wb.createCellStyle();
style3.setFont(font3);
Font font4 = wb.createFont();
font4.setFontHeightInPoints((short)18);
font4.setFontName("Wingdings");
font4.setColor(IndexedColors.GOLD.getIndex());
CellStyle style4 = wb.createCellStyle();
style4.setFont(font4);
Font font5 = wb.createFont();
font5.setFontName("Symbol");
CellStyle style5 = wb.createCellStyle();
style5.setFont(font5);
XSSFCell cell0 = sheet.createRow(0).createCell(1);
cell0.setCellValue("Default");
cell0.setCellStyle(style0);
XSSFCell cell1 = sheet.createRow(1).createCell(1);
cell1.setCellValue("Courier");
cell1.setCellStyle(style1);
XSSFCell cell2 = sheet.createRow(2).createCell(1);
cell2.setCellValue("Arial中文内容");
cell2.setCellStyle(style2);
XSSFCell cell3 = sheet.createRow(3).createCell(1);
cell3.setCellValue("Times New Roman");
cell3.setCellStyle(style3);
XSSFCell cell4 = sheet.createRow(4).createCell(1);
cell4.setCellValue("Wingdings");
cell4.setCellStyle(style4);
XSSFCell cell5 = sheet.createRow(5).createCell(1);
cell5.setCellValue("Symbol");
cell5.setCellStyle(style5);
// Write the output to a file
FileOutputStream fileOut;
try {
fileOut = new FileOutputStream(path);
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
XlsxUtil test = new XlsxUtil();
test.writeExcel2007("C:\\Documents and Settings\\不了了之\\桌面\\2007.xlsx");
}
}
访问地址:http://www.massapi.com/class/xs/XSSFWorkbook.html
首先举一个简单的例子, 这个例子说明了HTTP协议的工作方式:
假设有一个人(这个人就是浏览器), 这个人需要跟外部联系, 以获取自己需要的东西,
这个人跟外部的每一个人联系都必须通过信件的方式, 首先他在信封上写明对方的地址, 对方的姓名, 还有自己的地址和姓名
其中自己的地址就代表了URL中的域名, 姓名代表了端口号. 自己需要的东西就是具体的URL。
然后他把这封信自己投递到对方的信箱中. 当对方收到信件的时候会根据信封上的地址和姓名,把需要的物品投递到这个人的信箱中.
其中信封就对应了HTTP协议. 我们来看一个HTTP协议的内容:
GET http://www.mytest.com:6666/index.html HTTP/1.1
Accept: */*
Accept-Language: zh-cn
User-Agent: mytest
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Host: www.mytest.com:6666
上面就相当于发信人在信封上写的内容, 第一行写的是对方的地址, 还有自己需要的东西, 另外这个人还在信封上写了这么以个内容:HTTP/1.1
这个意思是说:亲,这个信封上的内容是按照我们一开始商量好的1.1的版本写的哦。
Accept: */* // 亲,发什么东西我都可以识别哦。
Accept-Language: zh-cn // 亲,回信记得用中文哦。
User-Agent: mytest // 我当前的环境状况
Accept-Encoding: gzip, deflate // 亲,你可以使用中通和申通还有普邮哦,其他的我收不了哦。
Connection: Keep-Alive // 来了先别走哦,我可能还有东西发给你哦。
Host: www.mytest.com:6666 // 亲,这是我的地址哦。
上面是HTTP协议中请求的过程, 回复的过程跟这个差不多就不多说了。
接下来我们说代理的过程.
现在我们希望当这个人跟外部发信的时候,不需要他自己跑过去发,而是有我们代理发出去,假设我们是邮局吧。
这个人以后发信的时候不管给谁发,都把信直接投到邮局,然后他就不用管了,当邮局拿到信件以后根据信封上的地址发给接收人,然后再把接收人的响应收回来发给发信人. HTTP代理充当的就是邮局的功能。
接下来我们先用一个最简单的代码来实现一个http代理, 这个代码要正确跑起来,需要一个软件Fiddler,
为什么要用邮局呢,因为我们这个小邮局只有一个工作人员,这个工作人员不识字,看不懂信封上写的是什么,
他只能把这个信件转交给更专业的邮局Fiddler. 此处我们只讲转发的过程, 因此可以不用理会Fiddler.
最重要的是不要被Fiddler影响接下来的过程。
先上代码:
第一步,打开一个SocketServer, 监听指定的端口, 当有请求到达的时候起一个新的线程响应为了尽可能的说明关键逻辑,我去掉了异常捕获和资源释放的代码
socketServer = new ServerSocket(PORT);
while(true)
{
// 我们这个工作人员在很努力的等待用户的到来
// 他甚至从来不休息, 要结束掉他唯一的方法就是kill掉他
final Socket socket = socketServer.accept();
Runnable job = new Runnable(){
public void run(){
// 终于有用户来投信了, 开始工作
service(socket);
}
};
threadPoolExecutor.execute(job);
}然后我们来看service方法:
socket.setSoTimeout(10000);
socket.setKeepAlive(false);
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
Socket remote = new Socket("127.0.0.1", 8888);
InputStream remoteInputStream = remote.getInputStream();
OutputStream remoteOutputStream = remote.getOutputStream();
// 把信发出去
copy(inputStream, remoteOutputStream, 4096);
remote.setSoTimeout(10000);
// 把对方的响应发给发信人
copy(remoteInputStream, outputStream, 4096);
service方法很简单, 核心代码只有10行, 整体来看,核心代码不足20行, 但是它确是可以运行的, 并且很正常.当然性能很差, 打开一个复杂的页面, 需要好几分钟. 但是用来说明http代理的工作方式, 却很容易明白.
在下一篇文章中我们再进一步扩展,增加这个工作人员的能力,现在他还很笨。
下面是完整的代码:
/*
* $RCSfile: SimpleHttpProxy1.java,v $$
* $Revision: 1.1 $
* $Date: 2013-1-14 $
*
* Copyright (C) 2008 Skin, Inc. All rights reserved.
*
* This software is the proprietary information of Skin, Inc.
* Use is subject to license terms.
*/
package test.com.skin.http.proxy;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* <p>Title: SimpleHttpProxy1</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* @author xuesong.net
* @version 1.0
*/
public class SimpleHttpProxy1
{
public static final int PORT = 6666;
public static final byte[] CRLF = new byte[]{0x0D, 0x0A};
/**
* @param args
*/
public static void main(String[] args)
{
ServerSocket socketServer = null;
BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<Runnable>(1024);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(512, 1024, 30000, TimeUnit.SECONDS, blockingQueue);
try
{
socketServer = new ServerSocket(PORT);
while(true)
{
try
{
final Socket socket = socketServer.accept();
Runnable job = new Runnable(){
public void run(){
service(socket);
}
};
threadPoolExecutor.execute(job);
}
catch(SocketTimeoutException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(socketServer != null)
{
try
{
socketServer.close();
}
catch(IOException e)
{
}
}
}
}
private static void service(Socket socket)
{
Socket remote = null;
try
{
socket.setSoTimeout(2000);
socket.setKeepAlive(false);
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
remote = new Socket("127.0.0.1", 8888);
InputStream remoteInputStream = remote.getInputStream();
OutputStream remoteOutputStream = remote.getOutputStream();
try
{
copy(inputStream, remoteOutputStream, 4096);
}
catch(SocketTimeoutException e)
{
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
remote.setSoTimeout(10000);
copy(remoteInputStream, outputStream, 4096);
}
catch(SocketTimeoutException e)
{
}
catch(Exception e)
{
e.printStackTrace();
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(socket != null)
{
socket.close();
}
}
catch(IOException e)
{
}
try
{
if(remote != null)
{
remote.close();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
private static void copy(InputStream inputStream, OutputStream outputStream, int bufferSize) throws IOException
{
int length = 0;
byte[] buffer = new byte[bufferSize];
while((length = inputStream.read(buffer, 0, bufferSize)) > -1)
{
outputStream.write(buffer, 0, length);
}
outputStream.flush();
}
}
三层结构式基于模块化程序设计的思想,为实现分解应用程序的需求,而逐渐形成的一种标准模式的模块划分方法。
2、为什么要用三层结构
个人理解:采用分层增强了类和类之间的独立性,在团队合作开发的过程中能大大提高了开发的速率;在后期的维护工作中更容易;在程序员写代码的过程中,不会因为某个小小的需求的改变而大整整个系统模块,只需要改有问题的那一层就好了,比如说当数据库要改变的时候,只需要调整数据库就好了,不需 要重新开发,只做一些简单的调整即可。但是三层也有缺点就是执行速度不够快,所以说并不是所有的系统都要用三层架构去实现。
3、三层架构分类
三层架构划分为表现层(UI,user interface)、业务逻辑层(BLL,Business Logic Layer)、数据访问层(DAL,Data Access Layer),这样有利于系统的开发、维护、部署,实现了“高内聚,低耦合”。采用分层方法,把问题划分开一个一个解决,易于控制,易于延展,易于分配资源。
4、三层结构的概念以及之间的关系
概念
表示层位于最上层,用于显示和接收用户提交的数据,为用户提供交互式的界面。表示层一般为Windows窗体应用程序或Web应用程序。
业务逻辑层是表示层和数据访问层之间沟通的桥梁,主要负责数据的传递和处理。
数据访问层主要实现对数据的读取、保存和更新等操作。
关系
下图是各层的工作流程
在三层结构中,各层之间相互依赖,表示层依赖于业务逻辑层,业务逻辑层依赖于数据访问层。
5、如何搭建三层结构?
创建
打开VS2012,新建一个解决方案,新建一个类库LoginSolution,命名为LoginDAL,如下图
添加一个类库,业务逻辑层
创建一个WinForm窗体,UI表示层
代码
Model层
namespace MyBookShop.Models
{
public class User
{
public User() { }
//编号属性
public int id;
public int Id
{
//属性id的get和set方法
get { return id; }
set { id = value; }
}
//登录名属性
private string loginId;
//属性loginId的get和set方法
public string LoginId
{
get { return loginId; }
set { loginId = value; }
}
//密码属性
private string loginPwd;
//loginPwd属性的get和set方法
public string LoginPwd
{
get { return loginPwd; }
set { loginPwd = value; }
}
//姓名属性
private string name;
//属性name的get和set方法
public string Name
{
get { return name; }
set { name = value; }
}
}
}
业务逻辑层BLL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyBookShop.DAL;
using MyBookShop.BLL;
using MyBookShop.Models;
namespace MyBookShop.BLL
{
public class UserManager
{
//登录验证
public static bool Login(string loginId, string loginPwd, out User validUser)
{
User user = UserService.GetUserByLoginId(loginId);
//没有该用户信息
if (user == null)
{
validUser = null;
return false;
}
//密码正确
if (user.LoginPwd == loginPwd)
{
validUser = user;
return true;
}
//密码错误
else
{
validUser = null;
return false;
}
}
}
}
数据访问层DAL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace MyBookShop.DAL
{
public class UserService
{
//根据登录名查询用户信息
public static User GetUserByLoginId(string LoginId)
{
string sql = "select * from Users where LoginId=@LoginId";
using (SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@LoginId", LoginId)))
{
if (reader.Read())
{
//找到该用户信息后,将其保存到User对象中,并返回该对象
User user = new User();
user.id = (int)reader["Id"];
user.LoginId = (string)reader["LoginId"];
user.LoginPwd = (string)reader["LoginPwd"];
reader.Close();
return user;
}
else
{
reader.Close();
//未找到该用户,返回null
return null;
}
}
}
}
}
表示层UI
界面
namespace LoginUI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
string userName = txtUserName.Text.Trim();
string password=txtPassword .Text;