给你个解决办法: 1、在excel中加入一个排序列A,自动排序列的值为1、2、3、4......,可以通过excel自动填充来完成。 2、把excel当成数据源,通过ado.net来读取它,直接在sql语句的where条件中通过A来过滤(如要从第5行起取数据:select * from [$TablenName] where A > 4)。ado.net操作excel: http://www.cnblogs.com/Jinglecat/archive/2006/08/15/477472.html
row index = 4开始呗, 被雷到了
哥们,你这个GridView做什么用得啊
string serverPath = @"D:\Server\"; string ClientPath = this.FileUpload1.PostedFile.FileName; string fileName = null; OleDbConnection oleConn = new OleDbConnection(); oleConn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;data source=" + serverPath + fileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1;\""; oleConn.Open(); DataTable dt = oleConn.GetSchema("Tables"); DataTableReader dtReader = new DataTableReader(dt); OleDbCommand dc = new OleDbCommand("", oleConn); OleDbDataAdapter da = null; DataSet ds = new DataSet(); while (dtReader.Read()) { dc.CommandText = "select * from [" + dtReader["Table_Name"] + "]"; da = new OleDbDataAdapter(dc); DataTable data_dt = new DataTable(dtReader["Table_Name"].ToString().Replace('$', ' ').Trim()); da.Fill(data_dt); ds.Tables.Add(data_dt); } 这是我得代码
那个是显示数据库记录的的.net一个控件的名字啊,那个不重要
你把SQL 的数据库的代码改成你那个不就好了吗, 我没有用过OLEDB
你把SQL 的数据库的代码改成你那个不就好了吗, 我没有用过OLEDB
public static DataTable ImportToDataTable(string filepath, string sheetname) { OleDbConnection connection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=" + filepath + ";Extended Properties=Excel 8.0;"); OleDbDataAdapter adapter = new OleDbDataAdapter("select * from [" + sheetname + "$]", connection); DataSet set = new DataSet(); connection.Open(); adapter.Fill(set); connection.Close(); return set.Tables[0]; } 用这个方法将EXCEL导入DATATABLE,不就可以处理了吗,
private bool ExportDataGridview(DataGridView gridView, bool isShowExcle)
{
if (gridView.Rows.Count == 0)
return false;
//建立EXCEL对象
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcle;
//生成地址字段名
for (int i = 0; i < gridView.ColumnCount; i++)
{
excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i <= gridView.RowCount - 1; i++)
{
for (int j = 0; j < gridView.ColumnCount; j++)
{
if (gridView[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = "" + gridView[j, i].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
}
}
}
return true;
} private void button1_Click(object sender, EventArgs e)
{
this.ExportDataGridview(dataGridView1, true);
}这个是 把 数据库里的记录导入到 EXECL里的, 如果你要确定从那行开始读取把 FOR语句的行数改下 I的初直 就可以了
1、在excel中加入一个排序列A,自动排序列的值为1、2、3、4......,可以通过excel自动填充来完成。
2、把excel当成数据源,通过ado.net来读取它,直接在sql语句的where条件中通过A来过滤(如要从第5行起取数据:select * from [$TablenName] where A > 4)。ado.net操作excel:
http://www.cnblogs.com/Jinglecat/archive/2006/08/15/477472.html
string serverPath = @"D:\Server\";
string ClientPath = this.FileUpload1.PostedFile.FileName;
string fileName = null;
OleDbConnection oleConn = new OleDbConnection();
oleConn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;data source=" + serverPath + fileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1;\"";
oleConn.Open();
DataTable dt = oleConn.GetSchema("Tables");
DataTableReader dtReader = new DataTableReader(dt);
OleDbCommand dc = new OleDbCommand("", oleConn);
OleDbDataAdapter da = null;
DataSet ds = new DataSet();
while (dtReader.Read())
{
dc.CommandText = "select * from [" + dtReader["Table_Name"] + "]";
da = new OleDbDataAdapter(dc);
DataTable data_dt = new DataTable(dtReader["Table_Name"].ToString().Replace('$', ' ').Trim());
da.Fill(data_dt);
ds.Tables.Add(data_dt);
}
这是我得代码
{ OleDbConnection connection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=" + filepath + ";Extended Properties=Excel 8.0;"); OleDbDataAdapter adapter = new OleDbDataAdapter("select * from [" + sheetname + "$]", connection); DataSet set = new DataSet(); connection.Open();
adapter.Fill(set);
connection.Close(); return set.Tables[0];
}
用这个方法将EXCEL导入DATATABLE,不就可以处理了吗,