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 代码的更多相关文章

  1. 用EPPlus 读取excel,代码出错, the given key is not present in the dictionary

    using (ExcelPackage ep = new ExcelPackage(new FileInfo(path))) { ExcelWorksheet ws = ep.Workbook.Wor ...

  2. JavaScript 导出Excel 代码

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

  3. POI解析Excel代码

    // 批量区域数据导入 @Action(value = "area_batchImport") public String batchImport() throws IOExcep ...

  4. Java中用JXL导出Excel代码详解

    jxl是一个韩国人写的java操作excel的工具, 在开源世界中,有两套比较有影响的API可供使用,一个是POI,一个是jExcelAPI.其中功能相对POI比较弱一点.但jExcelAPI对中文支 ...

  5. JS导出Excel 代码笔记

    var tableToExcel = (function () { var uri = 'data:application/vnd.ms-excel;base64,', template = '< ...

  6. java批量生成excel代码分享

    package com.test.util; /** * @author ocq * */ import java.io.FileOutputStream; import java.io.IOExce ...

  7. 将GridView中的数据导出到Excel代码与注意事项

    //gv:需要导出数据的GridView,filename:导出excel文件名 public void ExportToExcel(GridView gv, string filename) { s ...

  8. POI 导入excel 代码记录 方便以后粘贴

    import java.io.FileInputStream; import java.io.InputStream; import javax.annotation.Resource; import ...

  9. java 通过Apache poi导出excel代码demo实例

    package com.zuidaima.excel.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutput ...

随机推荐

  1. TCP程序设计

        在Java中使用Socket(套接字)完成TCP程序的开发,使用此类可以方便地建立可靠的.双向的.持续的.点对点的通信连接.     在Socket的程序开发中,服务器端使用ServerSoc ...

  2. VLC开发相关

    1. libvlc在release下时,会出错.解决办法 project->linker->optimization->references->  NOREF 2. Iibvl ...

  3. 并发容器之ConcurrentSkipListSet

    概要 本章对Java.util.concurrent包中的ConcurrentSkipListSet类进行详细的介绍.内容包括:ConcurrentSkipListSet介绍ConcurrentSki ...

  4. java多线程之ThreadLocal

    ThreadLocal为每个线程保存变量,以保证数据同步. package Thread.Common; import java.util.Random; import java.util.concu ...

  5. 查看oracle SID

    源地址:http://blog.sina.com.cn/s/blog_5f20c4740100dodl.html SID就是数据库的实例 select instance_name from  V$in ...

  6. DualPivotQuicksort 排序算法解析

    DualPivotQuicksort是JDK1.7开始的采用的快速排序算法. 一般的快速排序采用一个枢轴来把一个数组划分成两半,然后递归之. 大量经验数据表面,采用两个枢轴来划分成3份的算法更高效,这 ...

  7. [ActionScript 3.0] AS3实现滤镜叠加效果

    import flash.display.BitmapData; import flash.filters.BlurFilter; import flash.geom.ColorTransform; ...

  8. [Java] java中的接口定义

    在Java的通常规范中,对数据成员的修改要通过接口提供的方法进行(如下面示例中接口中的void learnMath(int hours)和void learnEnglish(int hours)),这 ...

  9. JAVA 回调

    一.定义        回调就是把函数指针做为参数传入,如函数A做为参数传入函数B,由B函数决定何时.何地调用函数A, function A() function B(A)    {         ...

  10. Log4Net IsInfoEnabled 一直 false 的问题

    1.概述log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介.本文主要是介绍如何在VisualStu ...