c# 操作excel2007,滿足條件的數據寫入oracle

luckysli 2012-04-20 04:07:35
Microsoft.Office.Interop.Excel.Application myExcel = new Microsoft.Office.Interop.Excel.Application();
object oMissing = System.Reflection.Missing.Value;
Workbook book1 = myExcel.Application.Workbooks.Open(filePath, oMissing, oMissing, oMissing, oMissing,oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
Worksheet sheet1 = (Worksheet)book1.Sheets[1];
Range rng1 = sheet1.get_Range("A:A", Type.Missing);//取出A1列单元格
for (int i = 0; i < rng1.Count; i++)
{
string a = rng1.Value.ToString(); //为什么这里取出来的值是system.object[,]
if (rng1.ToString() == "#1")
{
Response.Write("<script>alert('aaaaaaa!')</script>");
}
}

我想要的效果是取出A1列單元格所以的值進行比較,滿足條件的寫入數據庫。
...全文
38 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
熙风 2012-04-20
  • 打赏
  • 举报
回复
        /// <summary>
/// 解析Excel
/// </summary>
/// <param name="filePath"></param>
/// <param name="name"></param>
/// <returns></returns>
public static DataSet LoadDataFromExcel(string filePath, string name)
{
try
{
string strConn;
// strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + filePath + ";Extended Properties=Excel 8.0";
strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=No\"";
OleDbConnection OleConn = new OleDbConnection(strConn);
OleConn.Open();
string sql = "SELECT * FROM [" + name + "$]";//可是更改Sheet名称,比如sheet2,等等
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
DataSet OleDsExcle = new DataSet();
OleDaExcel.Fill(OleDsExcle, name);
OleConn.Close();
return OleDsExcle;
}
catch (Exception err)
{
MessageBox.Show("数据绑定Excel失败! 失败原因:" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return null;
}
}



返回DataSet直接操作dataSet

111,126

社区成员

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

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

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