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

 

在做到需求的时候,改变了上传的Excel的上传格式,就是从Excel的第三行开始读取数据,在sql链接的地方做的属性HRD=yes;只能过滤掉第一行,但是现在是要过滤掉前两行的内容。
emmmmmmmma
应该这样做
Method1: 在进行select的时候,加上sheet的范围: string strCom = "SELECT * FROM [" + sheetName + "$B2:E4] "; 就是上面的这样 $后面加上要读取数据的范围就ok了==》B2到E4的覆盖的数据, 但是,它是已B2为坐标原点,向右向下延伸,涉及到的当前右行和下行的数据是读不到的。 Method2: //下面i的初始值等于几,就是从第n+1行开始读取的 for (int i = n; i < dt.Rows.Count; i++) { string Col1 = dt.Rows[i][1].ToString(); //得到第一列 订单号: string Col2 = dt.Rows[i][2].ToString();//得到第2列 运单号 string Col3 = dt.Rows[i][3].ToString();//得到第三列 子单号 string Col4 = dt.Rows[i][4].ToString();//得到第4列 回签单号 }

  

posted @ 2018-11-02 10:21  ProZkb  阅读(264)  评论(0编辑  收藏  举报