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

猜你喜欢

转载自www.cnblogs.com/ZkbFighting/p/9894774.html