cl_yq
cl_yq
2016-10-21 04:37
采纳率: 88.2%
浏览 770
已采纳

excel导出问题 ·····下面这个方法哪不对?

 /// <summary>
        /// 把内存中的信息插入到Execl的Sheet表中
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="conn"></param>
        /// <param name="TableColumnNames"></param>
        /// <param name="SheetTableColumnNames"></param>
        /// <returns></returns>
        public void InsertToTable(string Name, string conn, DataTable TableColumnNames, List<string> SheetTableColumnNames)
        {
            string temp = " insert into [" + Name + "](";
            string temps = "";
            string tempe = "";
            OleDbParameter[] param = new OleDbParameter[SheetTableColumnNames.Count];
            foreach (DataRow dr in TableColumnNames.Rows)
            {
                for (int t = 0; t < SheetTableColumnNames.Count; t++)
                {
                    if (t == 0)
                    {
                        temps = "[" + SheetTableColumnNames[t] + "]";
                        tempe = "@" + TableColumnNames.Columns[t].ColumnName;
                    }
                    else
                    {
                        temps = temps + ",[" + SheetTableColumnNames[t] + "]";
                        tempe = tempe + ",@" + TableColumnNames.Columns[t].ColumnName;
                    }
                }
            }

            temp = temp + temps + ") values (" + tempe + ")";

            for (int b = 0; b < SheetTableColumnNames.Count; b++)
            {
                param[b] = new OleDbParameter("@" + TableColumnNames.Columns[b].ColumnName, OleDbType.VarChar, 255);
                param[b].SourceColumn = TableColumnNames.Columns[b].ColumnName;
            }


            try
            {
                using (OleDbConnection oledbconnection = new OleDbConnection(conn))
                {
                    oledbconnection.Open();
                    OleDbCommand oledbcommand = new OleDbCommand();
                    oledbcommand.Connection = oledbconnection;
                    oledbcommand.CommandType = CommandType.Text;
                    oledbcommand.CommandText = temp;

                    foreach (OleDbParameter p in param)
                        oledbcommand.Parameters.Add(p);

                    OleDbDataAdapter adp = new OleDbDataAdapter();
                    OleDbCommandBuilder cb = new OleDbCommandBuilder(adp);
                    DataSet ds = new DataSet();
                    adp.SelectCommand = oledbcommand;
                    adp.InsertCommand = oledbcommand;
                    int i = adp.Update(TableColumnNames);
                    oledbconnection.Close();
                }
            }
            catch (Exception)
            {

                throw;
            }


        }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • caozhy
    已采纳

    oledb只能读excel不能写,用npoi这个库去写excel

    点赞 评论
  • testcs_dn
    微wx笑 2016-10-21 13:05

    连接成功了吗?表名是怎么写的?
    参考:http://blog.csdn.net/testcs_dn/article/details/25509249?locationNum=1&fps=1

    点赞 评论
  • u013479571
    桔梗花语啊 2016-10-22 10:34

    为什么不采用Microsoft.Office.Interop.Excel进行读写,而且不会有版本和获取工作表名的问题;http://download.csdn.net/detail/u013479571/9660559

    点赞 评论

相关推荐