添加CSV连接到Excel与c# [英] Add CSV Connection to Excel with c#

查看:182
本文介绍了添加CSV连接到Excel与c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多方法,我想添加一个csv连接到excel与c#
我试过的东西:

I have tried many methods, I want to add a csv connection to excel with c# things I have tried:

string textpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string path = textpath + "\\filename.csv"







string csvconectionstring = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + textpath + ";Extensions=asc,csv,tab,txt;";
                    SaturnAddIn.getInstance().Application.ActiveWorkbook.Connections.Add2("csv", csvconectionstring,null, 6, true, false);







string csvconectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"text;HDR=Yes;FMT=Delimited";
                    SaturnAddIn.getInstance().Application.ActiveWorkbook.Connections.Add2("csv", csvconectionstring,null, 6, true, false); 






这是我获得的异常


this is the exception i get

System.ArgumentException was unhandled by user code
  HResult=-2147024809
  Message=The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
  Source=""
  StackTrace:
       at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
       at Microsoft.Office.Interop.Excel.Connections.Add2(String Name, String Description, Object ConnectionString, Object CommandText, Object lCmdtype, Object CreateModelConnection, Object ImportRelationships)
       at Saturn.DataImportForm.CouchImport() in c:\Users\dtaylor\Documents\Visual Studio 2013\Projects\Saturn\Saturn\Saturn\CouchImport.cs:line 43
       at Saturn.DataImportForm.ImportButton_Click(Object sender, EventArgs e) in c:\Users\dtaylor\Documents\Visual Studio 2013\Projects\Saturn\Saturn\Saturn\DataImportForm.cs:line 41
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  InnerException: 


推荐答案

Microsoft Jet OLE DB 4.0 TextFile连接字符串
< a href =http://www.connectionstrings.com/textfile/ =nofollow> TextFile连接字符串

Microsoft Jet OLE DB 4.0 TextFile Connection Strings TextFile Connect Strings

如果您要查看DataSet中的数据

if you want to view the data in a DataSet

string strFileName = textpath + "\\filename.csv";
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) +"; Extended Properties = \"Text;HDR=YES;FMT=Delimited\"");
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + System.IO.Path.GetFileName(strFileName), conn);
DataSet ds = new DataSet("Temp");
adapter.Fill(ds);
DataTable tb = ds.Tables[0];

这篇关于添加CSV连接到Excel与c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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