//流请求
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:\Users\AmyZeng\Desktop\1.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();
原文地址:https://www.cnblogs.com/zengwangjing/p/10676091.html