• MemoryStream请求与接收


    //流请求

    static void Main(string[] args)

    {
    Console.WriteLine("Hello World!");
    //Console.ReadLine();
    List<EB_LOG> logs=new List<EB_LOG>(){
    new EB_LOG (){ id="11111", name="22222"},
    new EB_LOG (){ id="11111", name="22222"},
    };
    MemoryStream fs = ToExcel<EB_LOG>(logs);
    using (var httpClient = new HttpClient())
    {
    //1.创建文件流
    //FileStream fsRead = new FileStream(@"C:UsersAmyZengDesktop1.txt", FileMode.Open);
    //2.创建缓冲区,正常情况下,是不会直接等于文件大小的。这里只有读,所以就这么干了。
    byte[] byteArray = fs.ToArray();
    fs.Read(byteArray, 0, byteArray.Length);
    //3.开始读取, 返回值是读取到的长度。
    //int r = fsRead.Read(bytes, 0, bytes.Lenght);

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri("http://localhost:58278接收地址"));
    webRequest.Method = "POST"; //POST
    webRequest.ContentType = "application/x-xls";
    webRequest.ContentLength = byteArray.Length;
    Stream newStream = webRequest.GetRequestStream();
    newStream.Write(byteArray, 0, byteArray.Length);
    newStream.Close();

    HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
    string sss = sr.ReadToEnd();
    //4.关闭释放流
    fs.Close();
    fs.Dispose();

    }
     
    }

    public static MemoryStream ToExcel<T>(List<T> list, string filePath = null)
    {
    var memoryStream = new MemoryStream();
     

    IWorkbook workbook = new HSSFWorkbook();
    string sheetName = typeof(T).Name;
    ISheet sheet = workbook.CreateSheet(sheetName);
    IRow headerRow = sheet.CreateRow(0);
    Type elementType = typeof(T);
    // handling header.

    int headerIndex = 0;
    elementType.GetProperties().ToList().ForEach(propInfo =>
    {
    ICell headerCell = headerRow.CreateCell(headerIndex);
    headerIndex = headerIndex + 1;
    headerCell.SetCellValue(propInfo.Name);


    });
    int rowIndex = 1;
    foreach (T item in list)
    {
    IRow dataRow = sheet.CreateRow(rowIndex);
    int rowcellIndex = 0;
    elementType.GetProperties().ToList().ForEach(propInfo =>
    {
    ICell cell = dataRow.CreateCell(rowcellIndex);

    string value = (propInfo.GetValue(item, null) ?? "").ToString();
    cell.SetCellValue(value);
    rowcellIndex++;
    });
    rowIndex++;
    }


    ///storage/emulated/0/DCIM
    //FileStream fs = new FileStream("/storage/emulated/0/DCIM/log.xls", FileMode.OpenOrCreate, FileAccess.ReadWrite);

    workbook.Write(memoryStream);

    //fs.Write(memoryStream.ToArray(), 0, (int)memoryStream.Length);
     
    //fs.Dispose();
    workbook = null;

    return memoryStream;
     }
     
     
     
    //接收代码

    string logpatch = “D:Logs”
    if (!string.IsNullOrEmpty(logpatch))
    {
    if (!System.IO.Directory.Exists(logpatch))
    {
    System.IO.Directory.CreateDirectory(logpatch);
    }
    }
    else
    {

    logpatch = System.AppDomain.CurrentDomain.BaseDirectory+ "Logs\";
    if (!System.IO.Directory.Exists(logpatch))
    {
    System.IO.Directory.CreateDirectory(logpatch);
    }
    }

    logpatch = logpatch + System.DateTime.Today.ToString("yyyyMMdd") + "\";
    if (!System.IO.Directory.Exists(logpatch))
    {
    System.IO.Directory.CreateDirectory(logpatch);
    }

    byte[] bufferSteam = new byte[context.Request.InputStream.Length];
    context.Request.InputStream.Read(bufferSteam, 0, bufferSteam.Length);
    string strPath = logpatch + DateTime.Now.ToString("yyyyMMddHHmmsss")+"_" + strStoreTillid + ".xls";
    FileStream fsSteam = new FileStream(strPath, FileMode.Create, FileAccess.Write);
    BinaryWriter bwSteam = new BinaryWriter(fsSteam);
    bwSteam.Write(bufferSteam);
    bwSteam.Close();
    fsSteam.Close();

     
     
  • 相关阅读:
    Dot Net WinForm 控件开发 (七) 为属性提下拉式属性编辑器
    WinForm 程序的界面多语言切换
    c#遍历HashTable
    Dot Net WinForm 控件开发 (三) 自定义类型的属性需要自定义类型转换器
    Dot Net WinForm 控件开发 (六) 为属性提供弹出式编辑对话框
    Dot Net WinForm 控件开发 (一) 写一个最简单的控件
    Dot Net WinForm 控件开发 (四) 设置属性的默认值
    Dot Net WinForm 控件开发 (二) 给控件来点描述信息
    Dot Net WinForm 控件开发 (八) 调试控件的设计时行为
    Dot Net WinForm 控件开发 (五) 复杂属性的子属性
  • 原文地址:https://www.cnblogs.com/zengwangjing/p/10676091.html
Copyright © 2020-2023  润新知