C#Excel文件上传生成.docx_第1页
C#Excel文件上传生成.docx_第2页
C#Excel文件上传生成.docx_第3页
C#Excel文件上传生成.docx_第4页
C#Excel文件上传生成.docx_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

public bool uploadFile(string path)/上?传? string dt = path.Split(); string cc = ; string strUploadUrl = /PolicyManager/PolicyImport.aspx;/批量?上?传?政t策?的?地?址 string strRequesMsg = string.Empty; ;/返回?的?数y据Y信?息 #region 获?取?批量?上?传?政t策?页3面?信?息 try string NO = textBox1.Text; string postlogin = txt_UserName=*&txt_password=*&text_code= + NO + &dl=%E7%99%BB+%E5%BD%95; http.ResponseEncoding = UTF-8; string str1 = http.PostString(/CallMeLogin.aspx, postlogin, /home/main.html); strRequesMsg = http.GetString(strUploadUrl, false, /index.aspx); cc = http.GetStrCookies(); catch return false; string sta = value=0-9A-z/+=+; Regex stat= new Regex(sta); MatchCollection statt=stat.Matches(strRequesMsg); string state = ; try state = statt0.Value.Replace(value=, ); catch return false; #endregion #region 生成http协-议头部?信?息 var boundary = - + DateTime.Now.Ticks.ToString(x); var sbHead = new StringBuilder(); var sbFoot = new StringBuilder(); /http协-议头 sbHead.Append(- + boundary); sbHead.Append(rn); sbHead.Append(Content-Disposition: form-data; name=_MSPVSTATE + ); sbHead.Append(rnrn); sbHead.Append(state); sbHead.Append(rn); sbHead.Append(- + boundary); sbHead.Append(rn); sbHead.Append(Content-Disposition: form-data; name=_MSPVSTATE_ZIP + ); sbHead.Append(rnrn); sbHead.Append(statt1.Value.Replace(value=, ); sbHead.Append(rn); sbHead.Append(- + boundary); sbHead.Append(rn); sbHead.Append(Content-Disposition: form-data; name=_VIEWSTATE + ); sbHead.Append(rnrnrn); sbHead.Append(- + boundary); sbHead.Append(rn); sbHead.Append(Content-Disposition: form-data; name=fupExcel; filename= + dtdt.Length - 1 + ); sbHead.Append(rn); sbHead.Append(Content-Type: application/vnd.ms-excel); sbHead.Append(rnrn); /http协-议尾2 sbFoot.Append(rn); sbFoot.Append(- + boundary); sbFoot.Append(rn); sbFoot.Append(Content-Disposition: form-data; name=btnImport); sbFoot.Append(rnrnrn); sbFoot.Append(Content-Disposition: form-data; name=ExcelName); sbFoot.Append(rnrnrn); sbFoot.Append(导?入?); sbFoot.Append(rn); sbFoot.Append(- + boundary + -); sbFoot.Append(rn); #endregion var dataHead = Encoding.GetEncoding(gb2312).GetBytes(sbHead.ToString(); var dataFoot = Encoding.GetEncoding(gb2312).GetBytes(sbFoot.ToString(); byte dataFile = null; FileStream excelStream = null; try excelStream = new FileStream(path, FileMode.Open, FileAccess.Read); dataFile = new byteexcelStream.Length; excelStream.Read(dataFile, 0, dataFile.Length); catch (Exception ex) return false; finally if (excelStream != null) excelStream.Close(); #region 执行D上?传?操作 try var uploadRequest = HttpWebRequest.Create(/PolicyManager/PolicyImport.aspx) as HttpWebRequest; uploadRequest.Method = POST; uploadRequest.ServicePoint.Expect100Continue = false; / 取?消?100-continue uploadRequest.CookieContainer = http.GetContainerFromString(cc, ); uploadRequest.ContentLength = dataHead.Length + dataFile.Length + dataFoot.Length; uploadRequest.Accept = text/html, application/xhtml+xml, */*; uploadRequest.Headers.Add(Accept-Language, zh-CN); uploadRequest.Referer = /policymanager/policyimport.aspx; uploadRequest.UserAgent = Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0); uploadRequest.Headers.Add(Accept-Encoding, gzip, deflate); uploadRequest.ContentType = multipart/form-data; boundary= + boundary; uploadRequest.Host = ; uploadRequest.AllowAutoRedirect = false; var reqStream = uploadRequest.GetRequestStream(); reqStream.Write(dataHead, 0, dataHead.Length); reqStream.Write(dataFile, 0, dataFile.Length); reqStream.Write(dataFoot, 0, dataFoot.Length); reqStream.Close(); var response = uploadRequest.GetResponse(); using (var resStream = response.GetResponseStream() using (var reader = new StreamReader(resStream, Encoding.GetEncoding(utf-8) strRequesMsg = reader.ReadToEnd(); response.Close(); Regex itemReg = new Regex(0-9+条?政t策?被?成功|导?入?到?数y据Y库a!?, RegexOptions.IgnoreCase); if (itemReg.IsMatch(strRequesMsg) return true; else return true; catch return false; #endregion private void button4_Click(object sender, EventArgs e) string path = textBox8.Text; if (uploadFile(path) = true) MessageBox.Show(上?传?成功|); else MessageBox.Show(上?传?失败); public void CreatExcel1(string path, DataTable dt)/生成Excel System.Reflection.Missing miss = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks; Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(C:UsersAdministratorDesktop8000yi模板?8000yi.xls); Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet; for (int i = 0; i dt.Rows.Count; i+) sheet.Cellsi + 2, 1 = CZ; sheet.Cellsi + 2, 2 = dt.Rowsi7.ToString(); sheet.Cellsi + 2, 3 = ; sheet.Cellsi + 2, 4 = dt.Rowsi8.ToString(); sheet.Cellsi + 2, 5 = dt.Rowsi10.ToString(); sheet.Cellsi + 2, 6 = ; sheet.Cellsi + 2, 7 = 单程; sheet.Cellsi + 2, 8 = dt.Rowsi11.ToString(); sheet.Cellsi + 2, 9 = dt.Rowsi11.ToString(); sheet.Cellsi + 2, 10 = ; sheet.Cellsi + 2, 11 = ; sheet.Cellsi + 2, 12 = B2B; sheet.Cellsi + 2, 13 = dt.Rowsi9.ToString(); sheet.Cellsi + 2, 14 = dt.Rowsi3.ToString(); sheet.Cellsi + 2, 15 = dt.Rowsi4.ToString(); sheet.Cellsi + 2, 16 = dt.Rowsi5.ToString(); sheet.Cellsi + 2, 17 = dt.Rowsi6.ToString(); sheet.Cellsi + 2, 18 = ; sheet.Cellsi + 2, 19

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论