try
{
string sSql = "";
oleDbConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + @";Extended ProPerties=""Excel 8.0;HDR=Yes;""";
oleDbConn.Open();
oleDbCmd.CommandType = CommandType.Text;
oleDbCmd.Connection = oleDbConn;
//写列名
sSql = "CREATE TABLE sheet1(";
DataTable dt = dt1.Copy();
dt.Columns.Remove("MGUID");
for (int i = 0; i < dt.Columns.Count; i++)
{
if (i < dt.Columns.Count - 1)
{
if (dt.Columns[i].DataType.Name == "String")
{
sSql += "[" + dt.Columns[i].ColumnName + "] Text,";
}
else if (dt.Columns[i].DataType.Name == "DateTime")
{
sSql += "[" + dt.Columns[i].ColumnName + "] Datetime,";
}
else
{
sSql += "[" + dt.Columns[i].ColumnName + "] Decimal,";
}
}
else
{
if (dt.Columns[i].DataType.Name == "String")
{
sSql += "[" + dt.Columns[i].ColumnName + "] Text)";
}
else if (dt.Columns[i].DataType.Name == "DateTime")
{
sSql += "[" + dt.Columns[i].ColumnName + "] DateTime)";
}
else
{
sSql += "[" + dt.Columns[i].ColumnName + "] Decimal)";
}
}
}
oleDbCmd.CommandText = sSql;
oleDbCmd.ExecuteNonQuery();
DataView dv = new DataView();
dv.Table = dt;
DataView dv1 = new DataView();
dv1.Table = dt1;
if (conditions != "")
{
dv.RowFilter = conditions;
dv1.RowFilter = conditions;
}
dt = dv.ToTable();
dt1 = dv1.ToTable();
string MGUIDs = "";
for (int j = 0; j < dt.Rows.Count; j++)
{
MGUIDs += ",'" + dt1.Rows[j]["MGUID"].ToString() + "'";
sSql = "INSERT INTO sheet1 VALUES(";
for (int i = 0; i < dt.Columns.Count; i++)
{
if (i < dt.Columns.Count - 1)
{
if (DBNull.Value.Equals(dt.Rows[j][i]))
{
sSql += "NULL,";
}
else
{
if (dt.Columns[i].DataType.Name == "Decimal")
{
sSql += dt.Rows[j][i].ToString() + ",";
}
else
{
sSql += "'" + dt.Rows[j][i].ToString() + "',";
}
}
}
else
if (DBNull.Value.Equals(dt.Rows[j][i]))
{
sSql += "NULL)";
}
else
{
if (dt.Columns[i].DataType.Name == "Decimal")
{
sSql += dt.Rows[j][i].ToString() + ")";
}
else
{
sSql += "'" + dt.Rows[j][i].ToString() + "')";
}
}
}
oleDbCmd.CommandText = sSql;
oleDbCmd.ExecuteNonQuery();
}
if (dt2 != null)
{
sSql = "CREATE TABLE sheet21(";
dt = dt2.Copy();
dt.Columns.Remove("MGUID");
dt.Columns.Remove("DGUID");
for (int i = 0; i < dt.Columns.Count; i++)
{
if (i < dt.Columns.Count - 1)
{
if (dt.Columns[i].DataType.Name == "String")
{
sSql += "[" + dt.Columns[i].ColumnName + "] Text,";
}
else if (dt.Columns[i].DataType.Name == "DateTime")
{
sSql += "[" + dt.Columns[i].ColumnName + "] Datetime,";
}
else
{
sSql += "[" + dt.Columns[i].ColumnName + "] Decimal,";
}
}
else
{
if (dt.Columns[i].DataType.Name == "String")
{
sSql += "[" + dt.Columns[i].ColumnName + "] Text)";
}
else if (dt.Columns[i].DataType.Name == "DateTime")
{
sSql += "[" + dt.Columns[i].ColumnName + "] DateTime)";
}
else
{
sSql += "[" + dt.Columns[i].ColumnName + "] Decimal)";
}
}
}
oleDbCmd.CommandText = sSql;
oleDbCmd.ExecuteNonQuery();
dv = new DataView();
dv.Table = dt2;
if (MGUIDs != "")
{
dv.RowFilter = "MGUID in(" + MGUIDs.Substring(1) + ")";
}
dt = dv.ToTable();
for (int j = 0; j < dt.Rows.Count; j++)
{
sSql = "INSERT INTO sheet1 VALUES(";
for (int i = 0; i < dt.Columns.Count; i++)
{
if (i < dt.Columns.Count - 1)
{
if (DBNull.Value.Equals(dt.Rows[j][i]))
{
sSql += "NULL,";
}
else
{
if (dt.Columns[i].DataType.Name == "Decimal")
{
sSql += dt.Rows[j][i].ToString() + ",";
}
else
{
sSql += "'" + dt.Rows[j][i].ToString() + "',";
}
}
}
else
if (DBNull.Value.Equals(dt.Rows[j][i]))
{
sSql += "NULL)";
}
else
{
if (dt.Columns[i].DataType.Name == "Decimal")
{
sSql += dt.Rows[j][i].ToString() + ")";
}
else
{
sSql += "'" + dt.Rows[j][i].ToString() + "')";
}
}
}
oleDbCmd.CommandText = sSql;
oleDbCmd.ExecuteNonQuery();
}
}
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
//断开连接
oleDbCmd.Dispose();
oleDbConn.Close();
oleDbConn.Dispose();
}
}
您可能感兴趣的文章: