|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
在CSDN里搜索一下“初学”两字,竟有三百余篇帖子(也许更多)。有些帖子说,有了asp的基础,只要15天就能很熟悉了,我甚感自己的愚钝。更多帖子是向大家请教初学者适合看书。两个多月的时间(当然平常杂事比较多。以下代码,基础包含了对Excel的一切基本操纵
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Data.OleDb;
usingSystem.Data;
usingSystem.Windows.Forms;
usingSystem.Threading;
namespaceGuideInto
{
publicclassDAL_File
{
publicstaticstringFilePath
{
get;
set;
}
///<summary>
///毗连Excel
///</summary>
///<returns></returns>
publicstaticOleDbConnectionConn()
{
stringconnStr="Provider=Microsoft.Jet.OLEDB.4.0;PersistSecurityInfo=True;DataSource="+FilePath+";ExtendedProperties=Excel8.0;";
OleDbConnectionconn=newOleDbConnection(connStr);
returnconn;
}
///<summary>
///读取一切事情薄
///</summary>
///<returns></returns>
publicstaticDataTableGetAllExeclWorkBook()
{
using(OleDbConnectionconn=Conn())
{
conn.Open();
DataTabledt=conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
returndt;
}
}
///<summary>
///依据Sql读取Excel中的数据
///</summary>
///<paramname="sql"></param>
///<returns></returns>
publicstaticDataSetQuery(stringsql)
{
using(OleDbConnectionconn=Conn())
{
conn.Open();
OleDbDataAdapterda=newOleDbDataAdapter(sql,conn);
DataSetds=newDataSet();
da.Fill(ds,"ds");
returnds;
}
}
///<summary>
///读取指定事情薄中的数据
///</summary>
///<paramname="sheetname"></param>
///<returns></returns>
publicstaticDataTableGetList(stringsheetname)
{
stringsql="select*from["+sheetname+"]";
returnQuery(sql).Tables[0];
}
///<summary>
///判别表指定事情薄是不是存在
///</summary>
///<paramname="sheetname"></param>
///<returns></returns>
publicstaticboolExists(stringsheetname)
{
try
{
stringsql="selecttop1*from["+sheetname+"]";
Query(sql);
returntrue;
}
catch(OleDbExceptionex)
{
if(ex.Message.Contains("不存在"))
returnfalse;
}
returnfalse;
}
///<summary>
///删除execl中指定事情薄
///</summary>
///<paramname="sheetname"></param>
publicstaticvoidDropSheet(stringsheetname)
{
using(OleDbConnectionconn=Conn())
{
using(OleDbCommandcmd=newOleDbCommand())
{
conn.Open();
cmd.Connection=conn;
cmd.CommandText="droptable"+sheetname;
cmd.CommandType=CommandType.Text;
cmd.ExecuteNonQuery();
}
}
}
///<summary>
///拔出数据到事情薄
///</summary>
///<paramname="dt"></param>
publicstaticvoidAddDateToSheet(DataTabledt,ProgressBarbar,Labellb)
{
using(OleDbConnectionconn=Conn())
{
conn.Open();
using(OleDbTransactiontrans=conn.BeginTransaction())
{
try
{
OleDbCommandcmd=newOleDbCommand();
StringBuilderstrSql;
if(dt.Rows.Count>1)
{
bar.Minimum=1;
bar.Maximum=dt.Rows.Count;
lb.Visible=bar.Visible=true;
}
for(intj=0;j<dt.Rows.Count;j++)
{
if(j<=bar.Maximum)
{
bar.Value=j+1;
lb.Text=(bar.Value*100/bar.Maximum).ToString()+"%";
}
Application.DoEvents();
cmd.Transaction=trans;
cmd.Connection=conn;
cmd.CommandType=CommandType.Text;
strSql=newStringBuilder();
strSql.Append("insertinto"+dt.TableName+"(");
for(inti=0;i<dt.Columns.Count;i++)
{
if(i!=dt.Columns.Count-1)
strSql.Append("["+dt.Columns[i].ColumnName+"],");
else
strSql.Append("["+dt.Columns[i].ColumnName+"]");
}
strSql.Append(")values(");
stringvalue;
for(inti=0;i<dt.Columns.Count;i++)
{
value=dt.Rows[j][i].ToString()==""?"null":dt.Rows[j][i].ToString();
if(!string.IsNullOrEmpty(value))
{
value=value.Replace("","");
}
if(GetSheetTypeByInt(dt.Columns[i].DataType.Name)&&i!=dt.Columns.Count-1&&value.Equals("null"))
strSql.Append(value+",");
elseif(GetSheetTypeByInt(dt.Columns[i].DataType.Name)&&i==dt.Columns.Count-1&&value.Equals("null"))
strSql.Append(value);
elseif(GetSheetTypeByInt(dt.Columns[i].DataType.Name)&&i!=dt.Columns.Count-1)
strSql.Append(""+value+",");
elseif(GetSheetTypeByInt(dt.Columns[i].DataType.Name)&&i==dt.Columns.Count-1)
strSql.Append(""+value+"");
elseif(!GetSheetTypeByInt(dt.Columns[i].DataType.Name)&&i!=dt.Columns.Count-1)
strSql.Append(value+",");
elseif(!GetSheetTypeByInt(dt.Columns[i].DataType.Name)&&i==dt.Columns.Count-1)
strSql.Append(value);
}
strSql.Append(")");
cmd.CommandText=strSql.ToString();
cmd.ExecuteNonQuery();
}
trans.Commit();
}
catch(Exceptionex)
{
trans.Rollback();
throwex;
}
}
}
}
//创立Execl事情薄
publicstaticvoidCreateExecl(DataTabledt)
{
using(OleDbConnectionconn=Conn())
{
conn.Open();
using(OleDbCommandcmd=newOleDbCommand())
{
StringBuilderstrSql=newStringBuilder();
strSql.Append("createtable"+dt.TableName+"(");
for(inti=0;i<dt.Columns.Count;i++)
{
if(i==dt.Columns.Count-1)
strSql.Append("["+dt.Columns[i].ColumnName+"]"+ReturnExeclType(dt.Columns[i].DataType.Name)+"null");
else
strSql.Append("["+dt.Columns[i].ColumnName+"]"+ReturnExeclType(dt.Columns[i].DataType.Name)+"null,");
}
strSql.Append(")");
cmd.Connection=conn;
cmd.CommandType=CommandType.Text;
cmd.CommandText=strSql.ToString();
cmd.ExecuteNonQuery();
}
}
}
///<summary>
///Net数据范例与Excel中的数据范例转换
///</summary>
///<paramname="SqlType"></param>
///<returns></returns>
privatestaticstringReturnExeclType(stringSqlType)
{
switch(SqlType.ToLower())
{
case"string":
//case"smalldatetime":
//case"datetime":
return"nvarchar(255)";
case"byte":
return"byte";
case"int32":
return"int";
case"decimal":
return"numeric";
case"tinyint":
return"byte";
case"smalldatetime":
return"datetime";
case"boolean":
return"bit";
default:
returnSqlType;
}
}
///<summary>
///Excel中的数据范例与Net数据范例转换
///</summary>
///<paramname="SqlType"></param>
///<returns></returns>
privatestaticboolGetSheetTypeByInt(stringsheettype)
{
switch(sheettype.ToLower())
{
case"string":
returntrue;
case"smalldatetime":
returntrue;
case"datetime":
returntrue;
default:
returnfalse;
}
}
}
}
我认为,可以通过更加简单的首次编译,而增加第二次编译的负担,来提高java的运行效率。只是将java源代码进行简单的等价转换,而不假设编译成某种虚拟机器的目标格式,而由本地编译器针对性的二次编译。 |
|