写给默默奋斗的程序员们...worksheet专版
命名空间导入using Excel;BIN中在com组件中加入MS EXCEL5.0组件
string path = Request.MapPath("upfile/");
string filepath = path + "xx.xls";
bool j = false;
Application ObjExcel = new Application();
Workbook ObjWorkBook;
Worksheet ObjWorkSheet = null;
ObjWorkBook = ObjExcel.Workbooks.Open(filepath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
ObjWorkSheet = (Worksheet)ObjWorkBook.Sheets[1];
for (int i = 0; i < ObjWorkSheet.UsedRange.Rows.Count; i++)
{
ExcelInfo excelInfo = new ExcelInfo();
Range cellB = (Range)ObjWorkSheet.Cells[i + 2 , 1];
else
{
break;
}
}
if (j)
this.Page.RegisterStartupScript("true", "<script>alert('上传数据成功');</script>");
else
{
this.Page.RegisterStartupScript("false", "<script>alert('无数据');</script>");
return;
}
ObjExcel.Workbooks.Close();
CS文件中方法
/// <summary>
/// 插入excel数据
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static Boolean worksuccess(ExcelInfo excelInfo)
{
int ok = 0;
string str = "insert into TB_excel(b,c,d)values('" + excelInfo.B + "','" + excelInfo.C + "','" + excelInfo.D + "')";
using (SqlConnection sqlCon = new SqlConnection(Database.conStr))
{
using (SqlCommand sqlCmd = new SqlCommand(str, sqlCon))
{
sqlCon.Open();
ok = sqlCmd.ExecuteNonQuery();
return ok > 0;
}
}
}