123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- using Spire.Xls;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
- namespace UAS_SOP
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- Control.CheckForIllegalCrossThreadCalls = false;
- }
- private void normalButton1_Click(object sender, EventArgs e)
- {
- DialogResult result = ChooseSop.ShowDialog();
- if (result == DialogResult.OK)
- {
- FilePath.Text = ChooseSop.FileName;
- }
- }
- Thread thread;
- SetLoadingWindow stw;
- private void normalButton2_Click(object sender, EventArgs e)
- {
- uploadfile();
- }
- void uploadfile()
- {
- Workbook workbook = new Workbook();
- workbook.LoadFromFile(FilePath.Text);
- workbook.ConverterSetting.JPEGQuality = 50;
- //workbook.ConverterSetting.XDpi = 600;
- //workbook.ConverterSetting.YDpi = 600;
- List<string> filename = new List<string>();
- Console.WriteLine("一共Sheet:" + workbook.Worksheets[0].Rows.Length);
- try
- {
- for (int i = 0; i < workbook.Worksheets.Count; i++)
- {
- workbook.Worksheets[i].SaveToImage(workbook.Worksheets[i].Name + ".jpg");
- filename.Add(workbook.Worksheets[i].Name + ".jpg");
- Console.WriteLine(workbook.Worksheets[i].Name + ".jpg");
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message+ex.StackTrace);
- }
- Dictionary<string, object> dic = new Dictionary<string, object>();
- dic.Add("em_name", "管理员");
- dic.Add("em_code", "ADMIN");
- dic.Add("caller", "ProductSOP");
- //for (int i = 0; i < filename.ToArray().Length; i++)
- //{
- // Console.WriteLine("上传:" + filename.ToArray()[i] + ".jpg");
- // 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);
- //}
- }
- /// <summary>
- /// 请求上传图片到阿里云
- /// </summary>
- /// <param name="url">上传地址</param>
- /// <param name="filepath">本地文件路径</param>
- /// <param name="dic">上传的数据信息</param>
- /// <returns></returns>
- public static bool UploadFilesToRemoteUrl(string url1, string filepath, Dictionary<string, object> dic)
- {
- try
- {
- string boundary = DateTime.Now.Ticks.ToString("x");
- byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1);
- request.Method = "POST";
- request.ContentType = "multipart/form-data; boundary=" + boundary;
- Stream rs = request.GetRequestStream();
- var endBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
- string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n" + "\r\n" + "{1}" + "\r\n";
- if (dic != null)
- {
- foreach (string key in dic.Keys)
- {
- rs.Write(boundarybytes, 0, boundarybytes.Length);
- string formitem = string.Format(formdataTemplate, key, dic[key]);
- byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
- rs.Write(formitembytes, 0, formitembytes.Length);
- }
- }
- string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n\r\n";
- {
- rs.Write(boundarybytes, 0, boundarybytes.Length);
- var header = string.Format(headerTemplate, "file", Path.GetFileName(filepath));
- var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
- rs.Write(headerbytes, 0, headerbytes.Length);
- using (var fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
- {
- var buffer = new byte[1024];
- var bytesRead = 0;
- while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
- {
- rs.Write(buffer, 0, bytesRead);
- }
- }
- var cr = Encoding.UTF8.GetBytes("\r\n");
- rs.Write(cr, 0, cr.Length);
- }
- rs.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
- var response = request.GetResponse() as HttpWebResponse;
- if (response.StatusCode == HttpStatusCode.OK)
- {
- return true;
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- return false;
- }
- }
- }
|