111,120
社区成员
发帖
与我相关
我的任务
分享
我最终解决方法就是用直接OPEN的方式打开读单元格,不过这样的效率确实差啊!像上面兄弟说用NPOI来操作,我也想的,但是我就不想携带那些DLL文件,所以放弃了,目前只知道用这个方法保险点了,下次好好研究那个NPOI才行。
[/quote]
估计是你输出的方式不一样吧,你可以像下面一位兄弟 geyewei 用textBox 或者richTextBOX输出看看
public static void Main(string[] args)
{
QueryCSV();
Console.ReadLine();
}
public static void QueryCSV()
{
//文件路径
string tableName = "file.csv";
string filePath = AppDomain.CurrentDomain.BaseDirectory;
string pContent = string.Empty;
OleDbConnection oledbConn = new OleDbConnection();
OleDbCommand oledbCmd = new OleDbCommand();
OleDbDataReader dataReader;
try
{
//两种连接方式皆可
//string strConnOledb = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=";
string strConnOledb="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
strConnOledb += filePath;
strConnOledb += ";Extended Properties='Text;HDR=Yes;IMEX=1;'";
oledbConn.ConnectionString = strConnOledb;
oledbConn.Open();
StringBuilder commandText = new StringBuilder("SELECT ");
commandText.AppendFormat("* From {0}", tableName);
oledbCmd.Connection = oledbConn;
oledbCmd.CommandText = commandText.ToString();
dataReader = oledbCmd.ExecuteReader();
while (dataReader.Read())
{
pContent = Convert.ToString(dataReader["content"]);
}
dataReader.Close();
}
catch (System.Exception ex)
{
oledbConn.Close();
}
finally
{
oledbConn.Close();
}
Console.WriteLine(pContent);
}

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm As New OpenFileDialog
With frm
.ShowDialog()
If .FileName <> "" Then
Dim strCon As String = ""
If .FileName.EndsWith(".xlsx") Then
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & .FileName & ";Extended Properties=""Excel 12.0 Xml;HDR=Yes"";"
Else
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & .FileName & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
End If
Dim strSQL As String = "SELECT * FROM [Sheet1$]"
Using adp As New Data.OleDb.OleDbDataAdapter(strSQL, strCon), dtbRet As New DataTable
adp.Fill(dtbRet)
TextBox1.Text = dtbRet.Rows(0)(0)
End Using
End If
End With
End Sub