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 ...
随机推荐
- Redis作者谈Redis应用场景(转)
毫无疑问,Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在如何把大象放进冰箱这样的问题上,而是利用Redis灵活多变的数据结构和数据操作,为不同的大象 ...
- [Java Web – 3A] – Spring MVC开发注意事项
1.使用Maven构建项目 2.SpringMVC 绝对路径的问题 首先要明确一点,在html中,资源文件也是有自己的URL,即href中是支持绝对路径.如下代码: <link href=&qu ...
- python(6)时间戳和北京时间互转,输出当前的时间和推到七天前的日期
项目发展的需要:(包含时间函数)time datetime 时间戳和北京时间互转 import time import datetime s = '2015-04-17 11:25:30' d = d ...
- js手机短信按钮倒计时
/* 120秒手机短信按钮倒计时 */ exports.sendmessage = function (name) { var second = 120; $(name). ...
- Word Frequency
https://leetcode.com/problems/word-frequency/ Write a bash script to calculate the frequency of each ...
- struts (二)
1.运行流程 client --->tomcat ---> webapp -->web.xml --->filter -->struts.xml ---->na ...
- selenium启动firefox、ie、chrome各浏览器方法
1.启动firefox浏览器 a.如果你的本地firefox是默认路径安装的话,如下方式即可启动浏览器 WebDriver driver = new FirefoxDriver(); driver.g ...
- 批处理操作mysql数据库
批处理操作mysql数据库 1.使用批处理自动登录mysql数据库 @echo offcd C:\program files\mysql\mysql server 5.5\binmysql -u ro ...
- 异步任务神器 Celery 简明笔记
转自:http://www.jianshu.com/p/1840035cb510 异步任务 异步任务是web开发中一个很常见的方法.对于一些耗时耗资源的操作,往往从主应用中隔离,通过异步的方式执行.简 ...
- Mysql 学习笔记 20140219
1. Mysql常用命令:每个命令以分号结束. create database name; 创建数据库 use databasename; 选择数据库 dr ...