本文介绍了添加CSV连接到Excel与c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我尝试了很多方法,我想添加一个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 getSystem.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连接字符串 TextFile连接字符串Microsoft Jet OLE DB 4.0 TextFile Connection StringsTextFile Connect Strings如果您要查看DataSet中的数据if you want to view the data in a DataSetstring 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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 08:01