导入Excel文件到SQL Server [英] Import Excel file into SQL Server

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

问题描述

我要导入Excel使用asp.net文件到SQL Server。当我点击链接,这应该弹出一个文件打开对话框。它应该只允许用户选择Excel文件( *。XLS,.XLSX * )。

我试图用,当我点击这个被调用的方法等一个boutton:

 保护无效btnBrowse_Click(对象发件人,EventArgs的发送)
{
    使用(打开文件对话框打开文件对话框=新的OpenFileDialog())
    {
        openFileDialog.Multiselect = FALSE;
        openFileDialog.Filter =数据源(的* .xls,* .xlsx)格式| * .xls的*; * XLSX。        如果(openFileDialog.ShowDialog()== DialogResult.OK)
        {
            txtFileName.Text = openFileDialog.FileName;
        }
    }
}

但它显示在 openFileDialog.ShowDialog()== DialogResult.OK 错误:当前线程必须设置为单线程单元(STA)模式OLE调用之前,制成。确保您的主要功能有请将STAThreadAttribute标示。如果调试器被连接到处理此异常仅引发。
任何想法吗?


解决方案

尝试使用常规验证前pression的文件扩展名限制到你想要的类型。

<$p$p><$c$c>ValidationEx$p$pssion=\"^(([a-zA-Z]:)|(\\\\{2}\\w+)\\$?)(\\\\(\\w[\\w].*))(.xls|.XLS|.xlsx|.XLSX)$\"

祝你好运!

I need to import an Excel file into SQL Server using asp.net. When I click on the link this should bring up a File Open dialog. It should only allow users to select Excel files (*.xls, *.xlsx).

I tried to used a boutton when i clicking on this it is calling a method like:

protected void btnBrowse_Click(object sender, EventArgs e)
{        
    using (OpenFileDialog openFileDialog = new OpenFileDialog())
    {
        openFileDialog.Multiselect = false;
        openFileDialog.Filter = "Data Sources (*.xls, *.xlsx)|*.xls*;*.xlsx";

        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            txtFileName.Text = openFileDialog.FileName;
        }
    }
}

but it is showing error on openFileDialog.ShowDialog() == DialogResult.OK: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process. Any idea please?

解决方案

Try using a Regular Validation Expression to limit the file extensions to your desired types.

ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.xls|.XLS|.xlsx|.XLSX)$"

Good luck!

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

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