关于文件导入的问题,急!!! 我想要将客户端的excel文件中的内容导入到服务器端的数据库中,能不能实现?如何实现呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 找个excel工具类就可以,excel文件内容的格式和数据库表结构要一致 我要去excel中固定单元格的字段插入到数据库中,怎么取数据呢? /// <summary> /// 将Excle数据导入数据库 /// Excle列名必须与要传入表的列名一致 /// </summary> /// <param name="strPath">路径</param> /// <param name="strTableName">表名</param> public static int InTableFromExcel(string strPath,string strTableName) { SqlServerDbAccess m_SqlServerDbAccess = new SqlServerDbAccess(); OleDbConnection cnnxls = null; OleDbCommand comm = null; OleDbDataAdapter myda = null; int x = 0; int intOpinion = 0; DataSet myds; try { //读取Excel string mystring = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strPath + ";" + "Extended Properties=Excel 8.0;"; cnnxls = new OleDbConnection(mystring); comm = new OleDbCommand("select * from [sheet1$]", cnnxls); myda = new OleDbDataAdapter(comm); myds = new DataSet(); myda.Fill(myds); //查看列名是否一直 string strOpinion = "SELECT TOP 0 * FROM " + strTableName; DataSet ds = new DataSet(); ds = m_SqlServerDbAccess.executeDataSetCmdString(strOpinion); System.Data.DataTable table = ds.Tables[0]; System.Data.DataTable excelTable = myds.Tables[0]; if (excelTable.Columns.Count > table.Columns.Count) { cnnxls.Close(); return 0; } foreach (DataColumn col in table.Columns) { for (int y = 0; y < excelTable.Columns.Count; y++) { if (col.ColumnName.Trim() == excelTable.Columns[y].ColumnName.Trim()) { intOpinion++; break; } } } if (intOpinion != excelTable.Columns.Count) { cnnxls.Close(); return 0; } //数据导入数据库 int intColumnCount = excelTable.Columns.Count; string strColumnName = excelTable.Columns[0].ColumnName; for (int i = 1; i < excelTable.Columns.Count; i++) { strColumnName += "," + excelTable.Columns[i].ColumnName; } string strCommText = string.Empty; for (int i = 0; i < myds.Tables[0].Rows.Count; i++) { strCommText += " INSERT INTO " + strTableName + "(" + strColumnName + ") values ( '"; for (int j = 0; j < intColumnCount - 1; j++) { strCommText += myds.Tables[0].Rows[i].ItemArray[j].ToString() + "', '"; } strCommText += myds.Tables[0].Rows[i].ItemArray[intColumnCount - 1].ToString() + "') "; } x = m_SqlServerDbAccess.executeNoQueryCmdString(strCommText); cnnxls.Close(); } catch (Exception ex) { cnnxls.Close(); } finally { cnnxls.Close(); } return x; }不知道可否用的上 标签上显示数据库表中的一个字段 怎么才能让他不显示该字段前几个字符 Jsp中的Serverlet和Asp.net里的ashx文件有木有一点像啊? 有木有! gridview 简单问题 asp.net 页面引用值问题 请问怎么能让DataList在一行绑定N个对象呀???? MVC2 的错误页面配置 请问-客户端执行输出服务端的数据问题(excel自定义格式化输出) DataSet创建的临时表,保存不了状态??-解决即给分 知道一个文件名,如何用程序获取此文件的全路径? 在哪里有jmail组件下载的 哪位有文件批量上传控件呀? 如何把数据库中存取的图片,取出后放到硬盘中
/// <summary>
/// 将Excle数据导入数据库
/// Excle列名必须与要传入表的列名一致
/// </summary>
/// <param name="strPath">路径</param>
/// <param name="strTableName">表名</param>
public static int InTableFromExcel(string strPath,string strTableName)
{
SqlServerDbAccess m_SqlServerDbAccess = new SqlServerDbAccess();
OleDbConnection cnnxls = null;
OleDbCommand comm = null;
OleDbDataAdapter myda = null;
int x = 0;
int intOpinion = 0;
DataSet myds;
try
{
//读取Excel
string mystring = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strPath + ";" + "Extended Properties=Excel 8.0;";
cnnxls = new OleDbConnection(mystring);
comm = new OleDbCommand("select * from [sheet1$]", cnnxls);
myda = new OleDbDataAdapter(comm);
myds = new DataSet();
myda.Fill(myds); //查看列名是否一直
string strOpinion = "SELECT TOP 0 * FROM " + strTableName;
DataSet ds = new DataSet();
ds = m_SqlServerDbAccess.executeDataSetCmdString(strOpinion);
System.Data.DataTable table = ds.Tables[0];
System.Data.DataTable excelTable = myds.Tables[0];
if (excelTable.Columns.Count > table.Columns.Count)
{
cnnxls.Close();
return 0;
}
foreach (DataColumn col in table.Columns)
{
for (int y = 0; y < excelTable.Columns.Count; y++)
{
if (col.ColumnName.Trim() == excelTable.Columns[y].ColumnName.Trim())
{
intOpinion++;
break;
}
}
}
if (intOpinion != excelTable.Columns.Count)
{
cnnxls.Close();
return 0;
} //数据导入数据库
int intColumnCount = excelTable.Columns.Count;
string strColumnName = excelTable.Columns[0].ColumnName;
for (int i = 1; i < excelTable.Columns.Count; i++)
{
strColumnName += "," + excelTable.Columns[i].ColumnName;
} string strCommText = string.Empty;
for (int i = 0; i < myds.Tables[0].Rows.Count; i++)
{
strCommText += " INSERT INTO " + strTableName + "(" + strColumnName + ") values ( '";
for (int j = 0; j < intColumnCount - 1; j++)
{
strCommText += myds.Tables[0].Rows[i].ItemArray[j].ToString() + "', '";
}
strCommText += myds.Tables[0].Rows[i].ItemArray[intColumnCount - 1].ToString() + "') ";
}
x = m_SqlServerDbAccess.executeNoQueryCmdString(strCommText);
cnnxls.Close();
}
catch (Exception ex)
{
cnnxls.Close();
}
finally
{
cnnxls.Close();
}
return x;
}不知道可否用的上