1. 程式人生 > >Excel 批量出來數據

Excel 批量出來數據

table reac sof load clas oid mex sta tar

 try
            {
                string sheetname = TextBox1.Text.Trim();

                HttpPostedFile upLoadPostFile = FileUpload1.PostedFile;
                string upLoadPath = FileUpload1.PostedFile.FileName;
                if (upLoadPath == "")
                {
                    ShowAlertMessage(
"請選擇上傳文件!"); return; } string excelType = upLoadPath.Split(.)[1].ToString(); if (excelType != "xls" && excelType != "xlsx") { ShowAlertMessage("此文件不是xls或者xlsx格式,請重新選擇上傳文件格式!"); }
else { InvoiceData.PutInvoiceJsonByExcel(sheetname, upLoadPostFile); ShowAlertMessage("發送開票請求完畢!"); } } catch (Exception ex) { CustomValidator1.ErrorMessage = ex.Message; CustomValidator1.IsValid
= false; }
public static void PutInvoiceJsonByExcel(string sheetName, HttpPostedFile upLoadPostFile)
        {
            try
            {
                //創建一個數據鏈接
                StringBuilder strCon = new StringBuilder();
                strCon.Append("Provider=Microsoft.ACE.OLEDB.12.0;");
                strCon.Append("Data Source=" + upLoadPostFile.FileName + ";");
                strCon.Append("Extended Properties=\"Excel 12.0;IMEX=1;\"");

                OleDbConnection myConn = new OleDbConnection(strCon.ToString());
                string strCom = "SELECT * FROM [" + sheetName + "$] ";
                myConn.Open();
                //打開數據鏈接,得到一個數據集
                OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
                DataSet myDataSet = new DataSet();
                //得到自己的DataSet對象
                myCommand.Fill(myDataSet, sheetName);

                myConn.Close();
                myConn.Dispose();
                foreach (DataRow dr in myDataSet.Tables[0].Rows)
                {
                    string strOrderSn = dr[0].ToString().Trim();
                    DataTable dt = OrderData.GetInvoiceByOrderSn(strOrderSn);
                    InvoiceData.PutInvoiceJson(dt);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("PutInvoiceJsonByExcel Error: " + ex.Message);
            }
        }

Excel 批量出來數據