Form1.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using Spire.Xls;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9. namespace UAS_SOP
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17. private void Form1_Load(object sender, EventArgs e)
  18. {
  19. Control.CheckForIllegalCrossThreadCalls = false;
  20. }
  21. private void normalButton1_Click(object sender, EventArgs e)
  22. {
  23. DialogResult result = ChooseSop.ShowDialog();
  24. if (result == DialogResult.OK)
  25. {
  26. FilePath.Text = ChooseSop.FileName;
  27. }
  28. }
  29. Thread thread;
  30. SetLoadingWindow stw;
  31. private void normalButton2_Click(object sender, EventArgs e)
  32. {
  33. uploadfile();
  34. }
  35. void uploadfile()
  36. {
  37. Workbook workbook = new Workbook();
  38. workbook.LoadFromFile(FilePath.Text);
  39. workbook.ConverterSetting.JPEGQuality = 50;
  40. //workbook.ConverterSetting.XDpi = 600;
  41. //workbook.ConverterSetting.YDpi = 600;
  42. List<string> filename = new List<string>();
  43. Console.WriteLine("一共Sheet:" + workbook.Worksheets[0].Rows.Length);
  44. try
  45. {
  46. for (int i = 0; i < workbook.Worksheets.Count; i++)
  47. {
  48. workbook.Worksheets[i].SaveToImage(workbook.Worksheets[i].Name + ".jpg");
  49. filename.Add(workbook.Worksheets[i].Name + ".jpg");
  50. Console.WriteLine(workbook.Worksheets[i].Name + ".jpg");
  51. }
  52. }
  53. catch (Exception ex)
  54. {
  55. Console.WriteLine(ex.Message+ex.StackTrace);
  56. }
  57. Dictionary<string, object> dic = new Dictionary<string, object>();
  58. dic.Add("em_name", "管理员");
  59. dic.Add("em_code", "ADMIN");
  60. dic.Add("caller", "ProductSOP");
  61. //for (int i = 0; i < filename.ToArray().Length; i++)
  62. //{
  63. // Console.WriteLine("上传:" + filename.ToArray()[i] + ".jpg");
  64. // UploadFilesToRemoteUrl("http://10.1.81.208:11773/mes/MEScommon/uploadFiles.action?_noc=1", @"C:\MES_NEW\mes-client\UAS_SOP\bin\Debug\" + filename.ToArray()[i], dic);
  65. //}
  66. }
  67. /// <summary>
  68. /// 请求上传图片到阿里云
  69. /// </summary>
  70. /// <param name="url">上传地址</param>
  71. /// <param name="filepath">本地文件路径</param>
  72. /// <param name="dic">上传的数据信息</param>
  73. /// <returns></returns>
  74. public static bool UploadFilesToRemoteUrl(string url1, string filepath, Dictionary<string, object> dic)
  75. {
  76. try
  77. {
  78. string boundary = DateTime.Now.Ticks.ToString("x");
  79. byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
  80. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1);
  81. request.Method = "POST";
  82. request.ContentType = "multipart/form-data; boundary=" + boundary;
  83. Stream rs = request.GetRequestStream();
  84. var endBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
  85. string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n" + "\r\n" + "{1}" + "\r\n";
  86. if (dic != null)
  87. {
  88. foreach (string key in dic.Keys)
  89. {
  90. rs.Write(boundarybytes, 0, boundarybytes.Length);
  91. string formitem = string.Format(formdataTemplate, key, dic[key]);
  92. byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
  93. rs.Write(formitembytes, 0, formitembytes.Length);
  94. }
  95. }
  96. string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n\r\n";
  97. {
  98. rs.Write(boundarybytes, 0, boundarybytes.Length);
  99. var header = string.Format(headerTemplate, "file", Path.GetFileName(filepath));
  100. var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
  101. rs.Write(headerbytes, 0, headerbytes.Length);
  102. using (var fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
  103. {
  104. var buffer = new byte[1024];
  105. var bytesRead = 0;
  106. while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  107. {
  108. rs.Write(buffer, 0, bytesRead);
  109. }
  110. }
  111. var cr = Encoding.UTF8.GetBytes("\r\n");
  112. rs.Write(cr, 0, cr.Length);
  113. }
  114. rs.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
  115. var response = request.GetResponse() as HttpWebResponse;
  116. if (response.StatusCode == HttpStatusCode.OK)
  117. {
  118. return true;
  119. }
  120. }
  121. catch (Exception e)
  122. {
  123. Console.WriteLine(e.Message);
  124. }
  125. return false;
  126. }
  127. }
  128. }