DataSet导出到Excel比较完整的解决方案(二)--服务器端生成文件(downmoon) (下)
2009-01-16 10:19:11 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁炬儳缍婇弻鐔兼⒒鐎靛壊妲紒鐐劤缂嶅﹪寮婚悢鍏尖拻閻庨潧澹婂Σ顔剧磼閻愵剙鍔ょ紓宥咃躬瀵鎮㈤崗灏栨嫽闁诲酣娼ф竟濠偽i鍓х<闁绘劦鍓欓崝銈囩磽瀹ュ拑韬€殿喖顭烽幃銏ゅ礂鐏忔牗瀚介梺璇查叄濞佳勭珶婵犲伣锝夘敊閸撗咃紲闂佺粯鍔﹂崜娆撳礉閵堝洨纾界€广儱鎷戦煬顒傗偓娈垮枛椤兘骞冮姀銈呯閻忓繑鐗楃€氫粙姊虹拠鏌ュ弰婵炰匠鍕彾濠电姴浼i敐澶樻晩闁告挆鍜冪床闂備胶绮崝锕傚礈濞嗘挸绀夐柕鍫濇川绾剧晫鈧箍鍎遍幏鎴︾叕椤掑倵鍋撳▓鍨灈妞ゎ厾鍏橀獮鍐閵堝懐顦ч柣蹇撶箲閻楁鈧矮绮欏铏规嫚閺屻儱寮板┑鐐板尃閸曨厾褰炬繝鐢靛Т娴硷綁鏁愭径妯绘櫓闂佸憡鎸嗛崪鍐簥闂傚倷娴囬鏍垂鎼淬劌绀冮柨婵嗘閻﹂亶姊婚崒娆掑厡妞ゃ垹锕ら埢宥夊即閵忕姷顔夐梺鎼炲労閸撴瑩鎮橀幎鑺ョ厸闁告劑鍔庢晶鏇犵磼閳ь剟宕橀埞澶哥盎闂婎偄娲ゅù鐑剿囬敃鈧湁婵犲﹤鐗忛悾娲煛鐏炶濡奸柍瑙勫灴瀹曞崬鈻庤箛鎾寸槗缂傚倸鍊烽梽宥夊礉鎼达絽鍨濇い鏍仜妗呴梺鍛婃处閸ㄦ壆绮婚幎鑺ュ€甸柨婵嗙凹缁ㄨ棄霉閻樿崵鐣烘慨濠冩そ濡啫鈽夊▎鎰€烽梺璇插閻噣宕¢幎鑺ュ仒妞ゆ洍鍋撶€规洖鐖奸、妤佸緞鐎n偅鐝┑鐘愁問閸n垳寰婇崜褉鍋撶粭娑樻搐缁犳煡鏌涢妷顔煎闁藉啰鍠栭弻锝夊棘閹稿孩鍠愰梺鑽ゅ枎缂嶅﹪寮诲☉鈶┾偓锕傚箣濠靛洨浜俊鐐€ら崜娆撴偋閸℃稈鈧棃宕橀鍢壯囧箹缁厜鍋撻懠顒€鍤紓鍌氬€风欢锟犲窗濡ゅ懎绠伴柟闂寸劍閸嬧晠鏌i幋锝嗩棄缁绢厸鍋撻梻浣虹帛閸旀洜绮旈棃娴虫盯宕橀鍏兼К闂侀€炲苯澧柕鍥у楠炴帡骞嬪┑鎰磻闁诲氦顫夐幐椋庣矆娓氣偓閸╃偤骞嬮敂钘変汗闂佸湱绮敮鈺傚閳ь剛绱撴担鐟板姢鐟滄壆鍋熼崚鎺戔枎閹惧疇鎽曞┑鐐村灦閻喖鈻介鍫熺厵閻熸瑥瀚慨鍥ㄣ亜閵夛妇绠炴慨濠冩そ閺屽懘鎮欓懠璺侯伃婵犫拃鍌氬祮闁哄瞼鍠栭幖褰掝敃閿濆懐锛撻梻浣瑰缁诲嫰宕戝☉銏犵厴闁瑰濮崑鎾绘晲鎼存ê浜炬い鎾寸⊕濞呭﹪鏌$仦鐣屝f繛纰变邯楠炲繒浠﹂挊澶婅厫闂傚倷鐒﹂惇褰掑磹閺囥垹绠犻柟閭﹀枟椤洟鏌熼幆褏鎽犲┑顖涙尦閺屾盯骞橀弶鎴犵シ闂佸憡鎸稿畷顒勨€旈崘顔嘉ч柛鈩冾殘娴犳悂姊洪懡銈呮毐闁哄懏鐩幃楣冩倻閽樺)銊ф喐婢舵劕纾婚柟鍓х帛閺呮煡骞栫划鐟板⒉闁诲繐绉瑰铏圭磼濡闉嶅┑鐐插级閿曘垺淇婇悽绋跨妞ゆ牗姘ㄩ悿鈧梻鍌氬€搁悧濠勭矙閹邦喛濮抽柤娴嬫櫇绾捐棄霉閿濆牊顥夐柣鎾村姈閹便劌螣缁嬪灝顬嬪┑鈥冲级閸旀瑩鐛Ο鍏煎珰闁肩⒈鍓﹀Σ浼存⒒娴gǹ鏆遍柟纰卞亰瀹曟劖绻濆В绋挎喘瀵埖鎯旈幘瀛樻澑婵$偑鍊栧濠氬Υ鐎n亶鍟呴柕澶涜礋娴滄粍銇勯幘璺轰粶婵℃彃顭烽弻锝夋晲閸パ冨箣濡ょ姷鍋炵敮锟犵嵁鐎n喖绫嶉柍褜鍓熼幃妤佺節濮橆厸鎷洪柣鐔哥懃鐎氼參宕曞Δ鍛厱婵☆垵銆€閸嬫捇鎮㈤幓鎺戠阀濠电姷鏁告慨鐑藉极閸涘﹥鍙忛柣鎴f閺嬩線鏌涘☉姗堟敾闁告瑥绻戦妵鍕箻閸楃偟浠肩紒鐐劤椤兘寮婚悢鐓庣鐟滃繒鏁☉銏$厓闂佸灝顑呴悘锕傛煏閸パ冾伃妤犵偞甯″畷鍗烆渻閹屾缂傚倸鍊搁崐椋庣矆娓氣偓钘濋梺顒€绉撮弸浣糕攽閻樿櫕鐨戠€规挷绶氶弻娑㈠焺閸愵亖濮囬梺绋匡功閸忔﹢寮诲☉妯锋斀闁糕剝顨忔导鈧俊鐐€栧褰掑礉閺囥垹鐓橀柟杈鹃檮閸婂鏌涢妷銏℃珖閺嶏繝姊绘担鍛婂暈闁圭ǹ顭烽幃鐑芥晜閻e备鏀虫繝鐢靛Т濞诧箓宕甸崘顔界厓闁告繂瀚弳鐔兼煥濞戞瑧鐭掓慨濠囩細閵囨劙骞掗幋婊冩瀳闂備礁鎲¢悷銉︻殽閹间礁鐓濋柟鐐灱閸亪鏌涢銈呮灁闁告ɑ鎮傞弻锝堢疀閺囩偘鎴风紒缁㈠幖閻栫厧鐣烽幋锕€绠婚悹鍥皺閻も偓濠电偠鎻徊浠嬪箟閿熺姴纾规い鏍仦閳锋垹鐥鐐村櫣濞存粌缍婇幃璺衡槈閺嵮冨Е闂佺硶鏂侀崑鎾愁渻閵堝棗绗掗柛鐕佸亰閹啫煤椤忓懐鍘告繛杈剧到濠€杈ㄦ櫠椤忓牊鐓冮悷娆忓閻忔挳鏌熼鐣屾噰鐎殿喖鐖奸獮瀣偐鏉堫煈鏁囬梻鍌氬€风粈浣革耿鏉堛劎浠氶梻浣侯攰婵倗鍒掓惔銊ョ闁圭儤顨呯猾宥夋煕椤愩倕鏋庡ù鐘烘缁辨挻鎷呴崜鎻掑壍濡炪倖娲樻繛濠囧极閸愵喖纾兼繛鎴炶壘楠炲牓姊绘担鍛婃儓婵炲眰鍨藉畷婵嗙暆閸曨剙鈧爼鏌eΟ鑲╁笡闁绘挻娲熼弻鐔兼嚋椤掆偓婵$厧霉濠婂嫬鍔ら柍瑙勫灴閺佸秹宕熼鈩冩線闂備胶枪閿曘儵鎮ч悩鑼殾婵犻潧顑嗛弲婵嬫煃瑜滈崜鐔煎灳閿曞倸閿ゆ俊銈傚亾闁绘帒鐏氶妵鍕箳瀹ュ牆鍘$紓浣哄Т婢т粙鍩€椤掆偓閸樻粓宕戦幘鏂ユ斀闁绘ǹ浜粣鏃堟煕鐎n偒娈旈柍瑙勫灴椤㈡瑧娑甸悜鐣屽弽婵犵數鍋涢幏鎴犲緤閸啣锝夊箛閺夎法顔婇梺鐟板暱绾绢參宕伴幘璇茬闁绘ḿ绮崵鎴︽煠缁嬭法浠涙慨锝嗗姍濮婂宕掑顑藉亾閻戣姤鍤勯柤鍝ユ暩娴犳碍绻濋悽闈涗粶妞ゆ洦鍙冨畷妤€螣娓氼垰娈ㄥ銈嗗姂閸婃牜鈧碍姘ㄩ埀顒傛嚀婢瑰﹪宕伴弽褉鏋旈柕濠忓缁♀偓闂佹眹鍨藉ḿ褎鐗庣紓浣哄亾濠㈡ḿ绮旈悷閭﹀殨闁哄被鍎辩粻鐢告煙閻戞ḿ绠橀柛鐐垫暬閺岋綁鎮╅悜姗嗕哗闁诲繐绻堥崝宀勵敊韫囨稑唯鐟滃宕戦幘鑸靛枂闁告洦鍓欑喊宥呪攽閳藉棗浜濈紒璇插€块敐鐐剁疀濞戞瑦鍎梺闈╁瘜閸橀箖鏁嶅⿰鍐f斀闁宠棄妫楅悘鐘绘煙绾板崬浜伴柨婵堝仜椤撳ジ宕堕埡鍐跨闯濠电偠鎻紞渚€藟閹捐绀夌€广儱顦伴悡娆戠磼鐎n亞浠㈤柡鍡涗憾閺岋綁鏁愰崶褍骞嬪Δ鐘靛仜椤戝寮崘顔肩劦妞ゆ帒鍊绘稉宥呪攽閻樺磭顣查柛瀣剁秮閺屾盯濡烽幋婵嗘殶濡ょ姴娲幃妤冩喆閸曨剙纰嶇紓浣割槹閹告娊鍨鹃弮鍫濈妞ゆ柨妲堣閺屾盯鍩勯崗鐙€浜Λ鍕吋閸モ晝锛濇繛杈剧到婢瑰﹪宕曢幇鐗堢厱闁靛ǹ鍎遍。宕囩磼椤旂⒈鍎忔い鎾冲悑瀵板嫮鈧綆浜栭崑鎾绘煥鐎c劋绨婚梺鐟版惈缁夊爼藝閿旈敮鍋撳▓鍨灈闁诲繑绻堥崺鐐哄箣閿曗偓閻擄繝鏌涢埄鍐炬畼濞寸媭鍨跺娲川婵犲海鍔堕梺鍛婃处閸欏骸煤閸涘﹣绻嗛柕鍫濈箳閸掍即鏌涢悤浣哥仸鐎规洘鍔欏畷褰掝敃閿濆懎浼庢繝纰樻閸ㄦ娊宕㈣缁傚秵銈i崘鈺佲偓鍨叏濡厧浜鹃悗姘炬嫹

主要的类文件如下:
ExcelReaderClass
/**//// <summary>
/// Summary description for ExcelReader.
/// </summary>
public class ExcelReader : IDisposable
{
Variables#region Variables
private int[] _PKCol;
private string _strExcelFilename;
private bool _blnMixedData = true;
private bool _blnHeaders = false;
private string _strSheetName;
private string _strSheetRange;
private bool _blnKeepConnectionOpen = false;
private OleDbConnection _oleConn;
private OleDbCommand _oleCmdSelect;
private OleDbCommand _oleCmdUpdate;
#endregion
properties#region properties
public int[] PKCols
{
get { return _PKCol; }
set { _PKCol = value; }
}
public string ColName(int intCol)
{
string sColName = "";
if (intCol < 26)
sColName = Agronet.Common.Framework.Util.SQLParser.StringParse(Convert.ToChar((Convert.ToByte((char)'A') + intCol)));
else
{
int intFirst = ((int)intCol / 26);
int intSecond = ((int)intCol % 26);
sColName = Agronet.Common.Framework.Util.SQLParser.StringParse(Convert.ToByte((char)'A') + intFirst);
sColName += Agronet.Common.Framework.Util.SQLParser.StringParse(Convert.ToByte((char)'A') + intSecond);
}
return sColName;
}
public int ColNumber(string strCol)
{
strCol = strCol.ToUpper();
int intColNumber = 0;
if (strCol.Length > 1)
{
intColNumber = Agronet.Common.Framework.Util.SQLParser.ShortParse(Convert.ToByte(strCol[1]) - 65);
intColNumber += Agronet.Common.Framework.Util.SQLParser.ShortParse(Convert.ToByte(strCol[1]) - 64) * 26;
}
else
intColNumber = Agronet.Common.Framework.Util.SQLParser.ShortParse(Convert.ToByte(strCol[0]) - 65);
return intColNumber;
}
public String[] GetExcelSheetNames()
{
System.Data.DataTable dt = null;
try
{
if (_oleConn == null) Open();
// Get the data table containing the schema
dt = _oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null) { return null; }
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
// Add the sheet name to the string array.
foreach (DataRow row in dt.Rows)
{
string strSheetTableName = row["TABLE_NAME"].ToString();
excelSheets[i] = strSheetTableName.Substring(0, strSheetTableName.Length - 1);
i++;
}
return excelSheets;
}
catch (Exception ex)//tony 2008.12.31 update
{
string s = ex.Message; return null;
}
finally
{
// Clean up.
if (this.KeepConnectionOpen == false)
{
this.Close();
}
if (dt != null)
{
dt.Dispose();
dt = null;
}
}
}
public string ExcelFilename
{
get { return _strExcelFilename; }
set { _strExcelFilename = value; }
}
public string SheetName
{
get { return _strSheetName; }
set { _strSheetName = value; }
}
public string SheetRange
{
get { return _strSheetRange; }
set
{
if (value.IndexOf(":") == -1) throw new Exception("Invalid range length");
_strSheetRange = value;
}
}
public bool KeepConnectionOpen
{
get { return _blnKeepConnectionOpen; }
set { _blnKeepConnectionOpen = value; }
}
public bool Headers
{
get { return _blnHeaders; }
set { _blnHeaders = value; }
}
public bool MixedData
{
get { return _blnMixedData; }
set { _blnMixedData = value; }
}
#endregion
Methods#region Methods
Excel Connection#region Excel Connection
private string ExcelConnectionOptions()
{
string strOpts = "";
if (this.MixedData == true)
strOpts += "Imex=1;";
if (this.Headers == true)
strOpts += "HDR=Yes;";
else
strOpts += "HDR=No;";
return strOpts;
}
private string ExcelConnection()
{
return
@"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data Source=" + _strExcelFilename + ";" +
@"Extended Properties=" + Convert.ToChar(34).ToString() +
@"Excel 8.0;" + ExcelConnectionOptions() + Convert.ToChar(34).ToString();
}
#endregion
Open / Close#region Open / Close
public void Open()
{
try
{
if (_oleConn != null)
{
if (_oleConn.State == ConnectionState.Open)
{
_oleConn.Close();
}
_oleConn = null;
}
if (System.IO.File.Exists(_strExcelFilename) == false)
{
throw new Exception("Excel file " + _strExcelFilename + "could not be found.");
}
_oleConn = new OleDbConnection(ExcelConnection());
_oleConn.Open();
}
catch (Exception ex)
{
throw ex;
}
}
public void Close()
{
if (_oleConn != null)
{
if (_oleConn.State != ConnectionState.Closed)
_oleConn.Close();
_oleConn.Dispose();
_oleConn = null;
}
}
#endregion
Command Select#region Command Select
private bool SetSheetQuerySelect()
{
try
{
if (_oleConn == null)
{
throw new Exception("Connection is unassigned or closed.");
}
if (_strSheetName.Length == 0)
throw new Exception("Sheetname was not assigned.");
/**//*
string tmpStr=@"SELECT * FROM ["
+ _strSheetName
+ "$" + _strSheetRange
+ "]";
*/
//System.Windows.Forms.MessageBox.Show(tmpStr);
//if(_strSheetName.EndsWith("$")){_strSheetName=_strSheetName.TrimEnd('$');}
_oleCmdSelect = new OleDbCommand(
@"SELECT * FROM ["
+ _strSheetName
+ "$" //+ _strSheetRange
+ "]", _oleConn);
//me
return true;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
simple utilities#region simple utilities
private string AddWithComma(string strSource, string strAdd)
{
if (strSource != "") strSource = strSource += ", ";
return strSource + strAdd;
}
private string AddWithAnd(string strSource, string strAdd)
{
if (strSource != "") strSource = strSource += " and ";
return strSource + strAdd;
}
#endregion
private OleDbDataAdapter SetSheetQueryAdapter(DataTable dt)
{
// Deleting in Excel workbook is not possible
//So this command is not defined
try
{
if (_oleConn == null)
{
throw new Exception("Connection is unassigned or closed.");
}
if (_strSheetName.Length == 0)
throw new Exception("Sheetname was not assigned.");
if (PKCols == null)
throw new Exception("Cannot update excel sheet with no primarykey set.");
if (PKCols.Length < 1)
throw new Exception("Cannot update excel sheet with no primarykey set.");
OleDbDataAdapter oleda = new OleDbDataAdapter(_oleCmdSelect);
string strUpdate = "";
string strInsertPar = "";
string strInsert = "";
string strWhere = "";
for (int iPK = 0; iPK < PKCols.Length; iPK++)
{
strWhere = AddWithAnd(strWhere, dt.Columns[iPK].ColumnName + "=?");
}
strWhere = " Where " + strWhere;
for (int iCol = 0; iCol < dt.Columns.Count; iCol++)
{
strInsert = AddWithComma(strInsert, dt.Columns[iCol].ColumnName);
strInsertPar = AddWithComma(strInsertPar, "?");
strUpdate = AddWithComma(strUpdate, dt.Columns[iCol].ColumnName) + "=?";
}
string strTable = "[" + this.SheetName + "$" + this.SheetRange + "]";
strInsert = "INSERT INTO " + strTable + "(" + strInsert + ") Values (" + strInsertPar + ")";
strUpdate = "Update " + strTable + " Set " + strUpdate + strWhere;
oleda.InsertCommand = new OleDbCommand(strInsert, _oleConn);
oleda.UpdateCommand = new OleDbCommand(strUpdate, _oleConn);
OleDbParameter oleParIns = null;
OleDbParameter oleParUpd = null;
for (int iCol = 0; iCol < dt.Columns.Count; iCol++)
{
oleParIns = new OleDbParameter("?", dt.Columns[iCol].DataType.ToString());
oleParUpd = new OleDbParameter("?", dt.Columns[iCol].DataType.ToString());
oleParIns.SourceColumn = dt.Columns[iCol].ColumnName;
oleParUpd.SourceColumn = dt.Columns[iCol].ColumnName;
oleda.InsertCommand.Parameters.Add(oleParIns);
oleda.UpdateCommand.Parameters.Add(oleParUpd);
oleParIns = null;
oleParUpd = null;
}
for (int iPK = 0; iPK < PKCols.Length; iPK++)
{
oleParUpd = new OleDbParameter("?", dt.Columns[iPK].DataType.ToString());
oleParUpd.SourceColumn = dt.Columns[iPK].ColumnName;
oleParUpd.SourceVersion = DataRowVersion.Original;
oleda.UpdateCommand.Parameters.Add(oleParUpd);
}
return oleda;
}
catch (Exception ex)
{
throw ex;
}
}
command Singe Value Update#region command Singe Value Update
private bool SetSheetQuerySingelValUpdate(string strVal)
{
try
{
if (_oleConn == null)
{
throw new Exception("Connection is unassigned or closed.");
}
if (_strSheetName.Length == 0)
throw new Exception("Sheetname was not assigned.");
_oleCmdUpdate = new OleDbCommand(
@" Update ["
+ _strSheetName
+ "$" + _strSheetRange
+ "] set F1=" + strVal, _oleConn);
return true;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
public void SetPrimaryKey(int intCol)
{
_PKCol = new int[1] { intCol };
}
public DataTable GetTable()
{
return GetTable("ExcelTable");
}
private void SetPrimaryKey(DataTable dt)
{
try
{
if (PKCols != null)
{
//set the primary key
if (PKCols.Length > 0)
{
DataColumn[] dc;
dc = new DataColumn[PKCols.Length];
for (int i = 0; i < PKCols.Length; i++)
{
dc[i] = dt.Columns[PKCols[i]];
}
dt.PrimaryKey = dc;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
public DataTable GetTable(string strTableName)
{
try
{
//Open and query
if (_oleConn == null) Open();
if (_oleConn.State != ConnectionState.Open)
throw new Exception("Connection cannot open error.");
if (SetSheetQuerySelect() == false) return null;
//Fill table
OleDbDataAdapter oleAdapter = new OleDbDataAdapter();
oleAdapter.SelectCommand = _oleCmdSelect;
DataTable dt = new DataTable(strTableName);
oleAdapter.FillSchema(dt, SchemaType.Source);
oleAdapter.Fill(dt);
if (this.Headers == false)
{
if (_strSheetRange.IndexOf(":") > 0)
{
string FirstCol = _strSheetRange.Substring(0, _strSheetRange.IndexOf(":") - 1);
int intCol = this.ColNumber(FirstCol);
for (int intI = 0; intI < dt.Columns.Count; intI++)
{
dt.Columns[intI].Caption = ColName(intCol + intI);
}
}
}
SetPrimaryKey(dt);
//Cannot delete rows in Excel workbook
dt.DefaultView.AllowDelete = false;
//Clean up
_oleCmdSelect.Dispose();
_oleCmdSelect = null;
oleAdapter.Dispose();
oleAdapter = null;
if (KeepConnectionOpen == false) Close();
return dt;
}
catch (Exception ex)
{
throw ex;
}
}
private void CheckPKExists(DataTable dt)
{
if (dt.PrimaryKey.Length == 0)
if (this.PKCols != null)
{
SetPrimaryKey(dt);
}
else
throw new Exception("Provide an primary key to the datatable");
}
public DataTable SetTable(DataTable dt)
{
try
{
DataTable dtChanges = dt.GetChanges();
if (dtChanges == null) throw new Exception("There are no changes to be saved!");
CheckPKExists(dt);
//Open and query
if (_oleConn == null) Open();
if (_oleConn.State != ConnectionState.Open)
throw new Exception("Connection cannot open error.");
if (SetSheetQuerySelect() == false) return null;
//Fill table
OleDbDataAdapter oleAdapter = SetSheetQueryAdapter(dtChanges);
oleAdapter.Update(dtChanges);
//Clean up
_oleCmdSelect.Dispose();
_oleCmdSelect = null;
oleAdapter.Dispose();
oleAdapter = null;
if (KeepConnectionOpen == false) Close();
return dt;
}
catch (Exception ex)
{
throw ex;
}
}
Get/Set Single Value#region Get/Set Single Value
public void SetSingleCellRange(string strCell)
{
_strSheetRange = strCell + ":" + strCell;
}
public object GetValue(string strCell)
{
SetSingleCellRange(strCell);
object objValue = null;
//Open and query
if (_oleConn == null) Open();
if (_oleConn.State != ConnectionState.Open)
throw new Exception("Connection is not open error.");
if (SetSheetQuerySelect() == false) return null;
objValue = _oleCmdSelect.ExecuteScalar();
_oleCmdSelect.Dispose();
_oleCmdSelect = null;
if (KeepConnectionOpen == false) Close();
return objValue;
}
public void SetValue(string strCell, object objValue)
{
try
{
SetSingleCellRange(strCell);
//Open and query
if (_oleConn == null) Open();
if (_oleConn.State != ConnectionState.Open)
throw new Exception("Connection is not open error.");
if (SetSheetQuerySingelValUpdate(objValue.ToString()) == false) return;
objValue = _oleCmdUpdate.ExecuteNonQuery();
_oleCmdUpdate.Dispose();
_oleCmdUpdate = null;
if (KeepConnectionOpen == false) Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (_oleCmdUpdate != null)
{
_oleCmdUpdate.Dispose();
_oleCmdUpdate = null;
}
}
}
#endregion
#endregion
public
Dispose / Destructor#region Dispose / Destructor
void Dispose()
{
if (_oleConn != null)
{
_oleConn.Dispose();
_oleConn = null;
}
if (_oleCmdSelect != null)
{
_oleCmdSelect.Dispose();
_oleCmdSelect = null;
}
// Dispose of remaining objects.
}
#endregion
CTOR#region CTOR
public ExcelReader()
{
//
// TODO: Add constructor logic here
//
}
#endregion
}
赞助商链接