收藏 分享(赏)

C#Excel文件上传生成.docx

上传人:fcgy86390 文档编号:5243972 上传时间:2019-02-13 格式:DOCX 页数:12 大小:19.08KB
下载 相关 举报
C#Excel文件上传生成.docx_第1页
第1页 / 共12页
C#Excel文件上传生成.docx_第2页
第2页 / 共12页
C#Excel文件上传生成.docx_第3页
第3页 / 共12页
C#Excel文件上传生成.docx_第4页
第4页 / 共12页
C#Excel文件上传生成.docx_第5页
第5页 / 共12页
点击查看更多>>
资源描述

1、public bool uploadFile(string path)/上?传?string dt = path.Split();string cc = “;string strUploadUrl = “http:/ strRequesMsg = string.Empty; ;/返回?的?数y据Y信?息#region 获?取?批量?上?传?政t策?页3面?信?息trystring NO = textBox1.Text;string postlogin = “txt_UserName=*http.ResponseEncoding = “UTF-8“;string str1 = http.Post

2、String(“http:/ postlogin, “http:/ = http.GetString(strUploadUrl, false, “http:/ = http.GetStrCookies();catchreturn false;string sta = “value=“0-9A-z/+=+“;Regex stat= new Regex(sta);MatchCollection statt=stat.Matches(strRequesMsg);string state = “;trystate = statt0.Value.Replace(“value=“, “);catchret

3、urn 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.A

4、ppend(“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);s

5、bHead.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(“Cont

6、ent-Type: application/vnd.ms-excel“);sbHead.Append(“rnrn“);/http协-议尾2sbFoot.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.

7、Append(“rnrnrn“);sbFoot.Append(“导?入?“);sbFoot.Append(“rn“);sbFoot.Append(“-“ + boundary + “-“);sbFoot.Append(“rn“);#endregionvar dataHead = Encoding.GetEncoding(“gb2312“).GetBytes(sbHead.ToString();var dataFoot = Encoding.GetEncoding(“gb2312“).GetBytes(sbFoot.ToString();byte dataFile = null;FileStre

8、am excelStream = null;tryexcelStream = new FileStream(path, FileMode.Open, FileAccess.Read);dataFile = new byteexcelStream.Length;excelStream.Read(dataFile, 0, dataFile.Length);catch (Exception ex)return false;finallyif (excelStream != null)excelStream.Close();#region 执行D上?传?操作tryvar uploadRequest =

9、 HttpWebRequest.Create(“http:/ as HttpWebRequest;uploadRequest.Method = “POST“;uploadRequest.ServicePoint.Expect100Continue = false; / 取?消?100-continueuploadRequest.CookieContainer = http.GetContainerFromString(cc, ““);uploadRequest.ContentLength = dataHead.Length + dataFile.Length + dataFoot.Length

10、;uploadRequest.Accept = “text/html, application/xhtml+xml, */*“;uploadRequest.Headers.Add(“Accept-Language“, “zh-CN“);uploadRequest.Referer = “http:/ = “ Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)“;uploadRequest.Headers.Add(“Accept-Encoding“, “gzip, deflate“);uploadReques

11、t.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, dataFo

12、ot.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策?被?成功|导?入?到?数

13、y据Y库a!?“, RegexOptions.IgnoreCase);if (itemReg.IsMatch(strRequesMsg)return true;elsereturn true;catchreturn false;#endregionprivate void button4_Click(object sender, EventArgs e)string path = textBox8.Text;if (uploadFile(path) = true)MessageBox.Show(“上?传?成功|“);elseMessageBox.Show(“上?传?失败“);public vo

14、id CreatExcel1(string path, DataTable dt)/生成ExcelSystem.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.Workb

15、ooks)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.R

16、ows.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.Cell

17、si + 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.Cells

18、i + 2, 17 = dt.Rowsi6.ToString();sheet.Cellsi + 2, 18 = “;sheet.Cellsi + 2, 19 = “;sheet.Cellsi + 2, 20 = “;sheet.Cellsi + 2, 21 = “;sheet.Cellsi + 2, 22 = “;sheet.Cellsi + 2, 23 = “;sheet.Cellsi + 2, 24 = “;sheet.Cellsi + 2, 25 = “;sheet.Cellsi + 2, 26 = “;sheet.Cellsi + 2, 27 = “;sheet.Cellsi + 2,

19、 28 = “;sheet.Cellsi + 2, 29 = “;sheet.Cellsi + 2, 30 = “;sheet.Cellsi + 2, 31 = “;book.SaveAs(path,Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing,Type.Missing, Type.Missing, Type.Missing,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);/关?闭?文?件tbook.Close(false, miss, miss);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 实用文档 > 办公软件应用

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报