//Get(flFile.FileName);
//int successCount = 0; //添加成功的条数
//int errorCount = 0; //失败条数
//string sbFailMachine = ""; //失败的机型
//string fileName = this.txtMsg.Value;
////处理excel
////Excel.Application excel = new Excel.Application();
////object oMissing = System.Reflection.Missing.Value;
////excel.Application.Workbooks.Open(fileName, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
////Excel.Workbook book = excel.Workbooks[1];
////Excel.Worksheet sheet = (Excel.Worksheet)excel.Worksheets[1];
////string strSheetName = sheet.Name;
//string strConn = "Provider=Microsoft.Jet.OLEDB.12.0;Data Source = " + fileName + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";
//string strExcel = "select * from [Sheet1$]"; //[" + strSheetName + "$]";
//DataSet ds = new DataSet();
//OleDbConnection conn = new OleDbConnection(strConn);
//conn.Open();
//OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, conn);
//adapter.Fill(ds);
//DataRow[] dr = ds.Tables[0].Select();
//int ss = ds.Tables[0].Rows.Count;
//int rowsnum = ds.Tables[0].Rows.Count;
//if (rowsnum == 0)
//{
// ClientScript.RegisterStartupScript(GetType(), "btnBatchAdd_Click", "alert('Excel表为空表,无数据!')", true);
//}
////循环excel数据
//for (int i = 1; i < rowsnum; i++)
//{
// Web_mobile mobile = new Web_mobile();
// mobile.M_name = dr[i][0].ToString();
// mobile.Classid = int.Parse(dr[i][1].ToString());
// mobile.M_softtype = int.Parse(dr[i][2].ToString());
// mobile.M_ishidden = "0";
// Web_mobileBLL bll = new Web_mobileBLL();
// if (bll.GetNameByName(mobile.M_name, int.Parse(mobile.Classid.ToString())) == null)
// {
// if (bll.Add(mobile) > 0)
// {
// //ClientScript.RegisterStartupScript(GetType(), "", "alert('添加成功');", true);
// successCount++;
// }
// else
// {
// //ClientScript.RegisterStartupScript(GetType(), "", "alert('添加失败,请重试');", true);
// sbFailMachine += mobile.M_name + "、";
// errorCount++;
// }
// }
// else
// {
// sbFailMachine += mobile.M_name + "、"; //显示未添加成功的机型名称
// }
//}
//if (sbFailMachine.ToString() != "")
//{
// ClientScript.RegisterStartupScript(GetType(), "fail", "alert('部分机型添加失败," + sbFailMachine.TrimEnd('、') + "机型添加失败!');", true);
//}
//else
//{
// ClientScript.RegisterStartupScript(GetType(), "", "alert('成功添加" + successCount + "条数据');", true);
//}
object missing = System.Reflection.Missing.Value;
Application excel = new Application();//lauch excel application
int successCount = 0; //添加成功的条数
int errorCount = 0; //失败条数
string sbFailMachine = ""; //失败的机型
if (excel == null)
{
Response.Write("<script>alert('Can't access excel')</script>");
}
else
{
excel.Visible = false; excel.UserControl = true;
// 以只读的形式打开EXCEL文件
Workbook wb = excel.Application.Workbooks.Open(strFileName, missing, true, missing, missing, missing,
missing, missing, missing, true, missing, missing, missing, missing, missing);
//取得第一个工作薄
Worksheet ws = (Worksheet)wb.Worksheets.get_Item(1);
//取得总记录行数 (包括标题列)
int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行数
//int columnsint = mySheet.UsedRange.Cells.Columns.Count;//得到列数
//取得数据范围区域 (不包括标题列)
Range rng1 = ws.Cells.get_Range("A2", "A" + rowsint); //c_name
Range rng2 = ws.Cells.get_Range("B2", "B" + rowsint); //c_id
Range rng3 = ws.Cells.get_Range("C2", "C" + rowsint); //m_sytemTerrace
object[,] arrayName = (object[,])rng1.Value2; //get range's value
object[,] arryItem = (object[,])rng2.Value2;
object[,] arryCus = (object[,])rng3.Value2;
//将新值赋给一个数组
string[,] arry = new string[rowsint - 1, 3];
for (int i = 1; i <= rowsint - 1; i++)
{
arry[i - 1, 0] = arrayName[i, 1].ToString();
//Item_Code列
arry[i - 1, 1] = arryItem[i, 1].ToString();
//Customer_Name列
arry[i - 1, 2] = arryCus[i, 1].ToString();
//添加入库
Web_mobile mobile = new Web_mobile();
mobile.M_name = arry[i - 1, 0];
mobile.Classid = int.Parse(arry[i - 1, 1]);
mobile.M_softtype = int.Parse(arry[i - 1, 2]);
mobile.M_ishidden = "0";
Web_mobileBLL bll = new Web_mobileBLL();
if (bll.GetNameByName(mobile.M_name, int.Parse(mobile.Classid.ToString())) == null)
{
if (bll.Add(mobile) > 0)
{
//ClientScript.RegisterStartupScript(GetType(), "", "alert('添加成功');", true);
successCount++;
}
else
{
//ClientScript.RegisterStartupScript(GetType(), "", "alert('添加失败,请重试');", true);
sbFailMachine += mobile.M_name + "、";
errorCount++;
}
}
else
{
sbFailMachine += mobile.M_name + "、"; //显示未添加成功的机型名称
}
}
//Response.Write(arry[0, 0] + " / " + arry[0, 1] + arry[0, 2] + "#" + arry[rowsint - 2, 0] + " / " + arry[rowsint - 2, 1] + arry[rowsint - 2, 2]);
if (sbFailMachine.ToString() != "")
{
ClientScript.RegisterStartupScript(GetType(), "fail", "alert('部分机型添加失败," + sbFailMachine.TrimEnd('、') + "机型添加失败!');", true);
}
else
{
ClientScript.RegisterStartupScript(GetType(), "", "alert('成功添加" + successCount + "条数据');", true);
}
}
excel.Quit(); excel = null;
Process[] procs = Process.GetProcessesByName("excel");
foreach (Process pro in procs)
{
pro.Kill();//没有更好的方法,只有杀掉进程
}
GC.Collect();
浙公网安备 33010602011771号