使用以下代码可以将Excel读入DataSet,怎样才可以将Txt度入DataSet,使用者的资料大多是Txt的,而且经常超过650000行,转成Excel反而比较麻烦,在Extended Properties后面加上Txt也不可以。string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=D:\\a.xls;" + "Extended Properties=Excel 8.0;";
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "ExcelInfo");
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=D:\\a.xls;" + "Extended Properties=Excel 8.0;";
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "ExcelInfo");
解决方案 »
- 命名空间中显示的元素无法定义为private
- C#2005 被调用的类中回调函数无法被运行
- c# winform 支持透明度的自定义panel,当拖动改变大小的时候怎样取消闪烁?控件代码如下:
- datagrid中数据不可改。。。
- 缺少Using指令或相应的程序集,我很纳闷,怎么会有这样的错误?在线等,分值100
- 多用户登录系统更改数据,如何及时刷新数据
- 自己写了个汉字选择器,希望用户选择了他要的汉字后,点击插入,将汉字插入到当前输入框中。选择器写好了,可如何将汉字赋值到任何制定输
- 关于正则表达式匹配无异常,资源耗尽的最佳解决方案
- 这个错误怎么解决:System.InvalidOperationExceptionSystem.InvalidOperationException: 该对象当前正在其他地方使用
- 怎么得到大于一个小数的最小整数???
- 一个byte数组处理并转换成short数组的问题
- [攒分贴]用QueryTable向excel批量导入数据
{
class Dataclass
{
comm c = new comm();
/// <summary>
/// 文檔數據讀入Dataset
/// </summary>
/// <param name = "tb">數據庫名</param>
/// <param name = "tablename">數據表名</param>
/// <param name = "path">文檔路徑</param>
/// <param name = "n">從文檔多少行開始作為數據</param>
public DataSet Tablebase(string tablename, string path, int n, ToolStripStatusLabel rowconut, ToolStripProgressBar progress)
{
try
{
StreamReader sr = new StreamReader(path);
System.Collections.ArrayList list = new System.Collections.ArrayList();
while (sr.Peek() > -1)
{
list.Add(sr.ReadLine());
}
sr.Close();
if (list.Count == 0)
{
MessageBox.Show("文檔中沒有數據行!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return null;
}
if (list.Count < n - 1)
{
MessageBox.Show("開始數據行超過範圍!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return null;
}
rowconut.Text = "行數:" + (list.Count - n).ToString();
Application.DoEvents();
progress.Minimum = 0;
progress.Maximum = list.Count - n;
DataSet ds = myset(tablename);
for (int i = 0; i < list.Count - n; i++)
{
string[] srvalue = list[n + i].ToString().Split(',');
DataRow row = ds.Tables[tablename].NewRow();
for (int j = 0; j < ds.Tables[tablename].Columns.Count; j++)
{
row[j] = srvalue[j];
}
ds.Tables[tablename].Rows.Add(row);
progress.Value = i;
}
int cd = ds.Tables[0].Rows.Count;
return ds;
}
catch (Exception err) { throw err; }
finally
{
c.sqlConn.Close();
} }
public DataSet myset(string tablename)
{
try
{
SqlDataAdapter da = new SqlDataAdapter("select * from " + tablename, c.sqlConn);
DataSet ds = new DataSet();
da.Fill(ds, tablename);
return ds;
}
catch (Exception err) { throw err; }
}
}
在这个里面找到了,现在可以了明天在研究wwwiii520,谢谢