1. 程式人生 > >Excel 批量出來資料

Excel 批量出來資料

 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);
            }
        }