不显示删除回复显示所有回复显示星级回复显示得分回复 急!!将Excel中的数据通过C#程序导入到Sql server中

PEPSI_ICE 2010-05-19 10:02:34
各位,请问谁做过将Excel中的数据通过C#程序导入到Sql server中呢?我现在遇到了一个问题,可是自己却解决不了了,希望给位能给点建议。。
下面的是我Excel的截图


下面是我的程序:
#region 该方法实现从Excel中导出数据到DataSet中,其中filepath为Excel文件的绝对路径, sheetname为excel文件的表名
public void ExcelDataSource(string filepath)
{
ArrayList excelList=ExcelSheetName(filepath);
for (int ii = 0; excelList != null && ii < excelList.Count;ii++ )
{
string sheetname = excelList[ii].ToString();
if (!sheetname.Contains("Print_Titles"))
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter oda = new OleDbDataAdapter("select * from [" + sheetname + "]", strConn);
DataSet ds = new DataSet();
oda.Fill(ds);
if (sheetname == "职务$")
{
xslTosql1(ds);
}
if (sheetname == "目录$")
{
xslTosql(ds);
}
}
}
}
#endregion

#region 获得Excel中的所有sheetname(工作单名)
public ArrayList ExcelSheetName(string filepath)
{
ArrayList excelList = new ArrayList();
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataTable dt = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
// (System.Data.OleDb.OleDbSchemaGuid.Tables, null);
conn.Close();
foreach (DataRow dr in dt.Rows)
{
excelList.Add(dr[2]);
}
return excelList;
}
#endregion

#region Excel数据导入SQL数据库(职务工作表单)
private void xslTosql1(DataSet ds)
{
for (int i = 2; i < ds.Tables[0].Rows.Count; i++)
{
string appointDate = ds.Tables[0].Rows[i]["任职时间"].ToString();
string removeDate = ds.Tables[0].Rows[i]["免职时间"].ToString();
string webSite = ds.Tables[0].Rows[i]["部门"].ToString();
string registerNumber = ds.Tables[0].Rows[i]["职务"].ToString();
string postNumber = ds.Tables[0].Rows[i]["批准文电号"].ToString();

//插入语句;
}
}
#endregion

#region Excel数据导入SQL数据库(目录工作表单)
private void xslTosql(DataSet ds)
{
for (int i = 1; i < ds.Tables[0].Rows.Count; i++)
{
string typeNum = ds.Tables[0].Rows[i]["类号"].ToString();
string fileName = ds.Tables[0].Rows[i]["材料名称"].ToString();
string webSite = ds.Tables[0].Rows[i]["年"].ToString();
string registerNumber = ds.Tables[0].Rows[i]["月"].ToString();
string postNumber = ds.Tables[0].Rows[i]["日"].ToString();
string themeNumber = ds.Tables[0].Rows[i]["份数"].ToString();
string grade = ds.Tables[0].Rows[i]["页数"].ToString();
string forumName = ds.Tables[0].Rows[i]["备注"].ToString();
//插入语句;
}
}
#endregion

我调试的时候,程序报错: 列“类号”不属于表 Table。 我知道是跟我Excel的结构有关系。但是我不知道该怎样去做,请给予帮助。。。谢谢。。
...全文
79 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
PEPSI_ICE 2010-05-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]
excel添加列
string typeNum ="";
代码还是要很多优化的
[/Quote]

请问怎样优化呢就我这个代码?
Justin-Liu 2010-05-19
  • 打赏
  • 举报
回复
现在好多灰太狼
wuyq11 2010-05-19
  • 打赏
  • 举报
回复
excel添加列
string typeNum ="";
代码还是要很多优化的

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧