错误:导出到Excel [英] error :Exporting to excel

查看:133
本文介绍了错误:导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我对此代码有疑问.我想将今天的日期直接导出为ex​​cel,但是出现了一些错误.

这是错误:
INSERT INTO语句中的语法错误

希望任何人都可以提出解决此错误的建议.

Hello,
I have problem with this code . I''m trying to export today date straightly to excel but some error have appear.

this is the error:
Syntax error in INSERT INTO statement

hope anybody can give suggestion to solve this error.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace test.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {


            
            try
            {
                System.Data.OleDb.OleDbConnection MyConnection;
                System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
                string sql = null;
                MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\\Users\\Administrator\\Desktop\\test.xlsx';Extended Properties=Excel 12.0;");
                
                string date = DateTime.Today.ToString();
                MyConnection.Open();
                myCommand.Connection = MyConnection;
                sql = "Insert into [Sheet1$] (DATE) values('"+date+"')";
                myCommand.CommandText = sql;
                myCommand.ExecuteNonQuery();
                MyConnection.Close();
            }
            catch (Exception ex)
            {

                error.Text = ex.Message;
            }
        }
    }
}

推荐答案

(DATE)值('" + date + ')"; myCommand.CommandText = sql; myCommand.ExecuteNonQuery(); MyConnection.Close(); } 捕获(例外) { error.Text = ex.Message; } } } }
(DATE) values('"+date+"')"; myCommand.CommandText = sql; myCommand.ExecuteNonQuery(); MyConnection.Close(); } catch (Exception ex) { error.Text = ex.Message; } } } }


请阅读以下文章:
使用OLEDB读写Excel [将数据导出到Excel [ 9将数据导出到Excel for ASP的解决方案. NET [ ^ ]

有关连接字符串的更多信息: http://www.connectionstrings.com/ [
Please, read these articles:
Reading and Writing Excel using OLEDB[^]
Exporting Data to Excel[^]
9 Solutions to Export Data to Excel for ASP.NET[^]

More about connection strings: http://www.connectionstrings.com/[^]


这篇关于错误:导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆