JCOM应用(二)

建立比较销售行为的一个简单的MySQL数据库的。它有三个表:
  · Sales是销售的细节信息,包括销售项、数量、销售价格、是谁销售的以及销售地区。
  · People包含销售人员的姓名和佣金。
  · Districts包含了销售地区的名称和税率。
生成报表的时候,你需要使用下面的业务逻辑来算出真正的数值:

  · 毛销售额等于销售项乘以销售价格加上地税金额。

  · 纯销售额等于毛销售额减去销售人员提取的税前佣金。

drop database if exists salesTable;
create database salesTable default character set utf8;

use salesTable;

drop table if exists sales;
create table sales(
  id int(11) primary key auto_increment,
  description varchar(225),
  quantity int(11),
  price double,
  district int(11),
  salesperson int(11)
);
insert into sales values(1,"clothes",10,10.0,1,1);

create table districts(
  id int(11) primary key auto_increment,
  districtname varchar(225),
  salestax double
);
insert into districts values(1,"guangzhou",0.1);

drop table if exists people;
create table  people(
  id int(11) primary key auto_increment,
  name varchar(255),
  commission double
);
insert into people values(1,"David",10);

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;
import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelApplication;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelRange;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbook;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbooks;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorksheet;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorksheets;


public class Sales {

	/**
	 * @param args
	 */
	public static void main(String[] args)throws Exception {
		// TODO Auto-generated method stub
//1.从数据库中获取数据并载入Excel
		ReleaseManager rm = new ReleaseManager();
		//IDispatch xlsApp = null;
		Connection conn = null;
		try{
			//建立Excel的引用
			//xlsApp = new IDispatch(rm,"Excel.Application");
			//IDispatch excel = (IDispatch)xlsApp.get("Workbooks");
			ExcelApplication excel = new ExcelApplication(rm);
			//使它可视
			excel.Visible(false);
			//xlsApp.put("Visible", new java.lang.Boolean(false));
			//打开即将要用的模版工作簿
			ExcelWorkbooks xlBooks = excel.Workbooks();
			ExcelWorkbook xlBook = xlBooks.Open("E:\\slovx\\java\\eclipse\\jcom\\source\\sales.xlsx");
			//获得修改的范围引用
			ExcelWorksheets xlSheets = xlBook.Worksheets();
			ExcelWorksheet xlSheet = xlSheets.Item(1);
			ExcelRange xlRange = xlSheet.Cells();
			
			//取数据
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost/" +
					"salesTable?user=root&password=");
			Statement stmt = conn.createStatement();
			ResultSet rs = null;
			String sql ="SELECT sales.id, sales.description, sales.quantity, sales.price, " +
					"districts.districtname,districts.salestax,people.name,people.commission " +
					"FROM sales,districts,people " +
					"WHERE sales.district = districts.id AND sales.salesperson = people.id " ;
			if(stmt.execute(sql))
			{
				rs = stmt.getResultSet();
			}
			int nColCount = rs.getMetaData().getColumnCount();
			int nRow = 1;
			int nCol = 0;
			while(rs.next())
			{
				nRow++;
				for(int i=1;i<=nColCount;i++)
				{
					xlRange.Item(nRow, i).Value(rs.getObject(i).toString());
				}
				//填入公式
				xlRange.Item(nRow, 9).Value("=RC[-6]*RC[-5]");
				xlRange.Item(nRow, 10).Value("=RC[-4]*RC[-1]/100");
				xlRange.Item(nRow, 11).Value("=RC[-2]+RC[-1]");
				xlRange.Item(nRow, 12).Value("=RC[-3]*((100-RC[-1])/100)");
			}
		}catch(Exception e){
		  e.printStackTrace();	
		}finally{
			if(rm != null){
				rm.release();
			}
			if(conn != null){
				conn.close();
			}
		}

	}

}


你可能感兴趣的:(java,Excel,jcom)