- UID
- 38377
- 积分
- 135
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2003-3-25
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
- [CommandMethod("mTest")]
- public void m_Test()
- {
- Editor m_ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
- string m_FilePath = @"d:\TestExcel.xls";
- #region 利用ADO.net操作Excel数据
- string m_ExcelConnectStr = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '" +
- m_FilePath + "';Extended Properties=Excel 8.0";
- OleDbConnection m_ODBC = new OleDbConnection(m_ExcelConnectStr);//连接Excel(如果给定文件不存在,自动创建新文件)
- string m_SheetName = "mkhSheet";
- #region 创建一个Excel表,并写入数据
- m_ODBC.Open();//打开数据库
- OleDbCommand m_ODBCMD = new OleDbCommand();
- m_ODBCMD.Connection = m_ODBC;
- string m_CMDStr = "";
- string[] m_TitleName = new string[] { "序号", "数据1", "数据2", "数据3", "数据4" };//表头(Excel表中第一行数据)
- string[] m_ValueType = new string[] { "numeric", "numeric", "numeric", "numeric", "numeric" };
- for (int i = 0; i < m_TitleName.GetLength(0); i++) m_CMDStr += m_TitleName【i】+ " " + m_ValueType【i】+ ",";
- m_CMDStr = "create table " + m_SheetName + " (" + m_CMDStr.Remove(m_CMDStr.Length - 1) + ")";
- m_ODBCMD.CommandText = m_CMDStr;//创建表
- try { m_ODBCMD.ExecuteNonQuery(); }//判断表是否已经存在?
- catch
- {
- m_ODBCMD.CommandText = "drop table " + m_SheetName; m_ODBCMD.ExecuteNonQuery();//如存在,则先删除
- m_ODBCMD.CommandText = m_CMDStr; m_ODBCMD.ExecuteNonQuery();//重新创建表
- }
- //向表中写入数据(增加记录)
- for (int i = 0; i < 6; i++)
- {
- m_CMDStr = "insert into " + m_SheetName + " (" + m_TitleName[0] + "," + m_TitleName[1] + "," + m_TitleName[3] + ") values(";
- m_CMDStr += i.ToString() + "," + (i + 1).ToString() + "," + (i + 2).ToString() + ")";
- m_ODBCMD.CommandText = m_CMDStr;//指定SQL语句
- m_ODBCMD.ExecuteNonQuery();//执行SQL语句,无返回值
- }
- //修改表中数据(修改记录)
- OleDbDataAdapter m_ODBDA = new OleDbDataAdapter("select * from [" + m_SheetName + "$]", m_ODBC);//读Excel文件的一个表
- DataSet m_DS = new DataSet();
- m_ODBDA.Fill(m_DS);//把表数据填入DataSet中
- System.Data.DataTable m_DT = m_DS.Tables[0];//获取表格所对应的DataTable对象
- for (int i = 1; i < 6; i++)
- {
- m_CMDStr = "update " + m_SheetName + " set " + m_TitleName[2] + "=";
- m_CMDStr += ((Convert.ToDouble(m_DT.Rows[i - 1][1]) + Convert.ToDouble(m_DT.Rows[1])) * 3.0 / 2).ToString("0.00");
- m_CMDStr += "," + m_TitleName[4] + "=";
- m_CMDStr += ((Convert.ToDouble(m_DT.Rows[i - 1][3]) + Convert.ToDouble(m_DT.Rows[3])) * 3.0 / 2).ToString("0.00");
- m_CMDStr += " where " + m_TitleName[0] + "=" + i.ToString();
- m_ODBCMD.CommandText = m_CMDStr;//指定SQL语句
- m_ODBCMD.ExecuteNonQuery();//执行SQL语句,无返回值
- }
- m_DT.Dispose(); m_DT = null;
- m_DS.Dispose(); m_DS = null;
- m_ODBDA.Dispose(); m_ODBDA = null;
- m_ODBCMD.Dispose(); m_ODBCMD = null;
- m_ODBC.Close();//关闭数据库
- #endregion
- #region 从Excel表中读取数据
- m_ODBDA = new OleDbDataAdapter("select * from [" + m_SheetName + "$]", m_ODBC);//读Excel文件的一个表
- m_DS = new DataSet();
- m_ODBDA.Fill(m_DS);//把数据填入DataSet中
- m_DT = m_DS.Tables[0];//获取表格所对应的DataTable对象,这样才能进行相关的操作
- string m_Str = "";
- for (int i = 0; i < m_DT.Columns.Count; i++) m_Str += m_DT.Columns.Caption.PadLeft(8,'-');
- m_ed.WriteMessage("{0}", m_Str);//显示表头(Excel工作表中第一行数据)
- for (int i = 0; i < m_DT.Rows.Count; i++)
- {
- m_Str = "";
- m_ed.WriteMessage("\n");
- for (int j = 0; j < m_DT.Columns.Count; j++)
- m_Str += m_DT.Rows[j].ToString().PadLeft(8,'-');
- m_ed.WriteMessage("{0}", m_Str);//显示每行数据
- }
- m_DT.Dispose(); m_DT = null;
- m_DS.Dispose(); m_DS = null;
- m_ODBDA.Dispose(); m_ODBDA = null;
- #endregion
- m_ODBC.Dispose(); m_ODBC = null;
- #endregion
- }
|
|