导入datadridview语句:
string tableName = "";
string Path = ofd.FileName;
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
tableName = schemaTable.Rows[0][2].ToString().Trim();
string strExcel = "Select * From [" + tableName + "]";
System.Data.DataTable excelDt = new System.Data.DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(strExcel, conn);
da.Fill(excelDt);
conn.Close();
MessageBox.Show("excel 导入成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (excelDt != null)
{
dataGridView1.DataSource = excelDt;
}
导出语句:
mycon.Open();
string sql = @"SELECT * INTO [abcef] IN 'D:\111.xls' 'Excel 8.0;' FROM data ";
OleDbCommand cmd = new OleDbCommand(sql, mycon);
cmd.ExecuteNonQuery();
mycon.Close();可不可以修改一下我的程序
让导入的第四列为两位小数的数值,
让导出的第四列为两位小数的数值,(1994.10不要变成1994.1)
string tableName = "";
string Path = ofd.FileName;
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
tableName = schemaTable.Rows[0][2].ToString().Trim();
string strExcel = "Select * From [" + tableName + "]";
System.Data.DataTable excelDt = new System.Data.DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(strExcel, conn);
da.Fill(excelDt);
conn.Close();
MessageBox.Show("excel 导入成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (excelDt != null)
{
dataGridView1.DataSource = excelDt;
}
导出语句:
mycon.Open();
string sql = @"SELECT * INTO [abcef] IN 'D:\111.xls' 'Excel 8.0;' FROM data ";
OleDbCommand cmd = new OleDbCommand(sql, mycon);
cmd.ExecuteNonQuery();
mycon.Close();可不可以修改一下我的程序
让导入的第四列为两位小数的数值,
让导出的第四列为两位小数的数值,(1994.10不要变成1994.1)
string str1=String.Format("{0:F}",d);
string result= String.Format("{0:N2}",Convert.ToDecimal("").ToString());
DEC(18,2)
//导入
if (excelDt != null)
{
if(excelDt.Rows.Count>0)
{
for(int i=0;i<excelDt.Rows.Count;i++)
{
execlDt.Rows[i][3]=execlDt.Rows[i][3].ToString("F2");
}
dataGridView1.DataSource = excelDt;
}
//导出
string sql = @"SELECT *,Convert(decimal(18,2),Number) INTO [abcef] IN 'D:\111.xls' 'Excel 8.0;' FROM data ";
//Number为你第四列在数据表里的字段名