Excel 代码
package com.chinabase.common.util; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement; import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;//下载jxl包 /**
* 操作Exl
* @author zhujl
*/
public class WorkExlChinaBase { private String writePath = "e://";
private String readPath = "e://";
private String templatePath = "e://"; /**
* 写入Exl
* @param 表名 列名 字段名
* @author zhujl
*/
public void writeExcel(String tableName, String[] names, String[] columnNames) throws Exception {
OutputStream os = null;
Connection conn = null;
ResultSet rs = null;
try {
// 构建Workbook对象
writePath += tableName + ".xls";
os = new FileOutputStream(writePath);
WritableWorkbook wwb = Workbook.createWorkbook(os);
// 构建Excel sheet
WritableSheet sheet = wwb.createSheet(tableName, 0);
Label name = null;
//A1 = 00 B1 = 10
//A2 = 01 B2 = 11
for(int i=0;i<names.length;i++){
name = new Label(i, 0, names[i]);
sheet.addCell(name);
}
conn = DateBase.getConnection();
//String sql = "select * from "+tableName+"";
//System.out.println("sql:"+sql);
String sql = "select * from client_info where co_number = '001'";
rs = DateBase.executeQuery(DateBase.getStmt(conn), sql);
Integer j = 1;
while(rs.next()){
Label val = null;
for(int i=0;i<columnNames.length;i++){
val = new Label(i, j, rs.getString(columnNames[i]));
sheet.addCell(val);
}
j++;
} DateBase.closeResultSet(rs);
DateBase.closeConn(conn);
//先调用write();再调用close();
wwb.write();
wwb.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
DateBase.closeResultSet(rs);
DateBase.closeConn(conn);
if (null != os) {
os.close();
}
}
} /**
* 读入Exl
* @param exl名 表名 字段名
* @author zhujl
*/
public void readExcel(String exlName, String tableName, String[] columnNames) throws Exception {
InputStream is = null;
Workbook workbook = null;
Connection conn = null;
Statement stmt = null;
try {
//readPath += exlName + ".xls";
System.out.println("exlName:"+exlName);
is = new FileInputStream(exlName);
workbook = Workbook.getWorkbook(is);
// sheet row column 下标都是从0开始的
Sheet sheet = workbook.getSheet(0);
//int column = sheet.getColumns();
//int row = sheet.getRows();
//System.out.println("共有" + row + "行," + column + "列数据");
StringBuffer sql = new StringBuffer();
//设置字段
for(int j=1;j<sheet.getRows();j++){
sql.append("insert into "+tableName+" (");
for(int i=0;i<columnNames.length;i++){
sql.append(columnNames[i]);
if(i != columnNames.length-1){
sql.append(",");
}
}
sql.append(") value (");
Cell val = null;
//设置值
for(int i=0;i<columnNames.length;i++){
val = sheet.getCell(i, j);
sql.append("'");
sql.append(val.getContents());
sql.append("'");
if(i != columnNames.length-1){
sql.append(",");
}
}
sql.append(")");
System.out.println(sql);
//conn = DateBase.getConnection();
//stmt = DateBase.getStmt(conn);
//stmt.execute(sql.toString());
//System.out.println("成功!");
sql.delete(0, sql.length());
} // 操作完成时,关闭对象,释放占用的内存空间
DateBase.closeStmt(stmt);
DateBase.closeConn(conn);
workbook.close();
is.close();
} catch (Exception e) {
e.printStackTrace(System.out);
} finally {
DateBase.closeStmt(stmt);
DateBase.closeConn(conn);
if (is != null) {
is.close();
}
}
}
/**
* 读入Exl
* @param exl名 表名 字段名
* @author xsc
*/
/*public String readExcelProductPart(String path, String tableName, String[] columnNames, String id,User user) throws Exception {
InputStream is = null;
Workbook workbook = null;
Connection conn = null;
Statement stmt = null;
String rd="";
try {
//readPath += exlName + ".xls";
is = new FileInputStream(path);
workbook = Workbook.getWorkbook(is);
// sheet row column 下标都是从0开始的
Sheet sheet = workbook.getSheet(0);
//int column = sheet.getColumns();
//int row = sheet.getRows();
//System.out.println("共有" + row + "行," + column + "列数据");
StringBuffer sql = new StringBuffer();
Boolean bl=true;
//设置字段
for(int j=1;j<sheet.getRows();j++){
sql.append("insert into "+tableName+" (");
for(int i=0;i<columnNames.length;i++){
sql.append(columnNames[i]);
if(i != columnNames.length-1){
sql.append(",");
}
}
sql.append(") value(");
Cell val = null;
for(int i=0;i<columnNames.length-3;i++){
val = sheet.getCell(i, j);
String content=val.getContents().replaceAll(" ","");
String hql = "select * from Part where code='"+content+"'";
conn = DateBase.getConnection();
stmt = DateBase.getStmt(conn);
ResultSet resultSet=stmt.executeQuery(hql.toString());
Part part=new Part();
if(resultSet.next()){
part.setId(Long.parseLong(resultSet.getString(1)));
}else{ rd+="第"+(val.getRow()+1)+"行,第"+(val.getColumn()+1)+"列,没有找到匹配零件.<br>";
bl=false;
}
sql.append("'");
sql.append(part.getId());
sql.append("'"); }
sql.append(","+user.getId()+",now(),"+id+")");
//System.out.println("----------------"+rd);
//System.out.println("+++++++++++"+sql);
if(bl){
conn = DateBase.getConnection();
stmt = DateBase.getStmt(conn);
stmt.execute(sql.toString());
//System.out.println("成功!");
sql.delete(0, sql.length());
} }
// 操作完成时,关闭对象,释放占用的内存空间
DateBase.closeStmt(stmt);
DateBase.closeConn(conn);
workbook.close();
is.close();
} catch (Exception e) {
e.printStackTrace(System.out);
} finally {
DateBase.closeStmt(stmt);
DateBase.closeConn(conn);
if (is != null) {
is.close();
}
}
return rd;
}*/
/**
* 读入Exl
* @param exl名 表名 字段名
* @author zhujl
*/
public void readExcelPart(String path, String tableName, String[] columnNames, String categoryID) throws Exception {
InputStream is = null;
Workbook workbook = null;
Connection conn = null;
Statement stmt = null;
try {
//readPath += exlName + ".xls";
is = new FileInputStream(path);
workbook = Workbook.getWorkbook(is);
// sheet row column 下标都是从0开始的
Sheet sheet = workbook.getSheet(0);
//int column = sheet.getColumns();
//int row = sheet.getRows();
//System.out.println("共有" + row + "行," + column + "列数据");
StringBuffer sql = new StringBuffer();
//设置字段
for(int j=1;j<sheet.getRows();j++){
sql.append("insert into "+tableName+" (");
for(int i=0;i<columnNames.length;i++){
sql.append(columnNames[i]);
if(i != columnNames.length-1){
sql.append(",");
}
}
sql.append(",created,updated,category_id) value (");
Cell val = null;
//设置值
for(int i=0;i<columnNames.length;i++){
val = sheet.getCell(i, j);
sql.append("'");
sql.append(val.getContents());
sql.append("'");
if(i != columnNames.length-1){
sql.append(",");
}
}
sql.append(",now(),now(),"+categoryID+")");
System.out.println(sql);
conn = DateBase.getConnection();
stmt = DateBase.getStmt(conn);
stmt.execute(sql.toString());
//System.out.println("成功!");
sql.delete(0, sql.length());
} // 操作完成时,关闭对象,释放占用的内存空间
DateBase.closeStmt(stmt);
DateBase.closeConn(conn);
workbook.close();
is.close();
} catch (Exception e) {
e.printStackTrace(System.out);
} finally {
DateBase.closeStmt(stmt);
DateBase.closeConn(conn);
if (is != null) {
is.close();
}
}
} /**
* 写入Exl模板
* @param 表名 字段名
* @author zhujl
*/
public void templateExcel(String tableName, String[] names) throws Exception {
OutputStream os = null;
try {
// 构建Workbook对象
templatePath += tableName + ".xls";
os = new FileOutputStream(templatePath);
WritableWorkbook wwb = Workbook.createWorkbook(os);
// 构建Excel sheet
WritableSheet sheet = wwb.createSheet(tableName, 0);
Label name = null;
//A1 = 00 B1 = 10
//A2 = 01 B2 = 11
for(int i=0;i<names.length;i++){
name = new Label(i, 0, names[i]);
sheet.addCell(name);
}
//先调用write();再调用close();
wwb.write();
wwb.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != os) {
os.close();
}
}
} public static void main(String[] args) {
/*String a = "[LOG] 充值失败1:ORA-01403: 未找到任何数据\nORA-01403: 未找到任何数据\nORA-06512:在 'ETONE.TRIG_INSERT_ZFDATA', line 27\nORA-04088: 触发器 'ETONE.TRIG_INSERT_ZFDATA' 执行过程中出错\nORA-06512: 在 'ETONE.INSERTCZJL', line 51\nORA-06512: 在 line 1\n - 11544 ms";
a = a.replace("'", "");
System.out.println(a);*/
WorkExlChinaBase workExl = new WorkExlChinaBase();
String [] columnNames = {"client_name","leaguer_no","sale","save_money","saveg_money","work_money","workg_money"};
String [] names = {"姓名","卡号","折扣","金额","赠送金额","工时金额","赠送工时金额"};
//String [] columnNames = {"client_name","leaguer_no","save_money","saveg_money","work_money"};
try {
workExl.writeExcel("client_info", names, columnNames);
//workExl.templateExcel("menu", names);
//workExl.readExcel("e://client_info.xls","menu",columnNames);
System.out.println("完成!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
Excel 代码的更多相关文章
- 用EPPlus 读取excel,代码出错, the given key is not present in the dictionary
using (ExcelPackage ep = new ExcelPackage(new FileInfo(path))) { ExcelWorksheet ws = ep.Workbook.Wor ...
- JavaScript 导出Excel 代码
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...
- POI解析Excel代码
// 批量区域数据导入 @Action(value = "area_batchImport") public String batchImport() throws IOExcep ...
- Java中用JXL导出Excel代码详解
jxl是一个韩国人写的java操作excel的工具, 在开源世界中,有两套比较有影响的API可供使用,一个是POI,一个是jExcelAPI.其中功能相对POI比较弱一点.但jExcelAPI对中文支 ...
- JS导出Excel 代码笔记
var tableToExcel = (function () { var uri = 'data:application/vnd.ms-excel;base64,', template = '< ...
- java批量生成excel代码分享
package com.test.util; /** * @author ocq * */ import java.io.FileOutputStream; import java.io.IOExce ...
- 将GridView中的数据导出到Excel代码与注意事项
//gv:需要导出数据的GridView,filename:导出excel文件名 public void ExportToExcel(GridView gv, string filename) { s ...
- POI 导入excel 代码记录 方便以后粘贴
import java.io.FileInputStream; import java.io.InputStream; import javax.annotation.Resource; import ...
- java 通过Apache poi导出excel代码demo实例
package com.zuidaima.excel.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutput ...
随机推荐
- cf519D . A and B and Interesting Substrings 数据结构map
题意: 已知26个小写字母有各自的权值(正,负,或0) 现在给出一个字符串,长度<=1e5 问这个字符串有多少个子串满足: 开头的字母和结尾的字母一样 字符串除了开头和结尾的字母外,其余的字母的 ...
- KEIL编译错误总结:
1 expected an identifier:#define宏定义常量后,如果再用前面定义的常量作为枚举常量就会报错,方法,去掉#define宏定义
- Java多线程之线程结束清理
该事例说明了清理工作必须要放在finally块中 package Thread.Interrupting; import java.util.concurrent.TimeUnit; class Ne ...
- TableViewCell的分割线显示不全解决方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath ...
- Access“存储过程"参数顺序要与执行代码生成的参数顺序一致
OleDbParameter olp; OleDbCommand cmd = new OleDbCommand("insertYjsData"); olp = new OleDbP ...
- iOS学习笔记总结整理
来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...
- Golang 图片上绘制文字
之前介绍过使用 FreeType-go 来绘制字(http://www.cnblogs.com/ghj1976/p/3445568.html), 现在相关的包被做了整合,主要是整合到了 github ...
- weblogic诊断案例-AdminServer平均1-2周崩溃
OS2台:RH5.5 64位 WEBLOGIC VERSION:9.2.3 JDK:1.5 64位 weblogic一个管理服务,4个受管(2台服务器做集群,每台服务器2个受管).应用部署后通过一个月 ...
- 网络编程socket
socket socket解释 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄. 应用程序通常通过"套接字"向网络发出请求或者应答 ...
- [转]使用 HttpClient 和 HtmlParser 实现简易爬虫
http://www.ibm.com/developerworks/cn/opensource/os-cn-crawler/ http://blog.csdn.net/dancen/article/d ...