文件上传下下载(不包含断点续传) Excel,Word导入导出基础
2019-03-23 10:09 .net小跟班(杜) 阅读(353) 评论(0) 编辑 收藏 举报1、文件上传下载(MVC应用)
视图:form表单,编码方式为multipart/form-data
<body> <div> <form action="/Demo/FileUpload" enctype="multipart/form-data" method="post"> <input id="f1" type="file" name="f1" /> <input id="Submit1" type="submit" value="上传" /> </form> <a href="/Demo/FileDownLoad">下载</a> </div> </body>
控制器
public ActionResult FileUpLoad(HttpPostedFileBase f1) { string path = Server.MapPath("~/Picture"); string filename = Path.Combine(path,f1.FileName); f1.SaveAs(filename); return Content("OK"); } public ActionResult FileDownLoad() { string path = Server.MapPath(@"C:\Users\Administrator\Desktop\自己练习\上传下载\UpLoad\UpLoad\Picture\QQ图片20190228155326.png"); FileStream fs = new FileStream(path,FileMode.Open); return File(fs,"text/plain","a.jpg"); }
2、将Excal导入datatable(控制台应用)
static void Main(string[] args) { Workbook wk = new Workbook(@"C:\Users\admin\Desktop\user.xlsx"); //获取数据所在的单元格 Cells cells = wk.Worksheets[0].Cells; for (int i = 0; i < cells.MaxDataRow; i++) { for (int j = 0; j < cells.MaxDataColumn; j++) { Console.Write(cells[i,j].Value+"\t"); } } Console.ReadKey(); }
3、提取数据库数据到Excel(控制台应用)
//提取数据到Excal static void Main(string[] args) { //读取证书 License license = new License(); license.SetLicense(@"C:\Users\admin\Desktop\自己练习\表达式树\从数据库中读取数据到Excal\Aid\License.lic"); DataTable dt = GetDataTable(); //全新的工作簿 Workbook wb = new Workbook(); //工作表 Worksheet ws = wb.Worksheets[0]; ws.Cells.ImportDataTable(dt,true,"A1"); wb.Save(@"C:\Users\admin\Desktop\\user.xlsx",SaveFormat.Xlsx); Console.WriteLine("读取成功"); Console.ReadKey(); } public static DataTable GetDataTable() { string sql = "select * from Student"; return DBHelper.GetDataTable(sql); }
4、Word读取(控制台应用)
static void Main(string[] args) { using (StreamReader reader = new StreamReader(@"D:\《雪中悍刀行》作者:烽火戏诸侯.txt", Encoding.Default)) { string line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } } Console.ReadKey(); }
5、Word写入(控制台应用)
static void Main(string[] args) { if (File.Exists(@"C:\Users\Administrator\Desktop\新建 Microsoft Word 文档")) { Console.WriteLine("文件已存在"); } FileStream fs = new FileStream(@"C:\Users\Administrator\Desktop\新建 Microsoft Word 文档", FileMode.Create); StreamWriter sw = new StreamWriter(fs); sw.WriteLine("床前明月光"); sw.WriteLine("疑是地上霜"); sw.Close(); fs.Close(); Console.WriteLine("写入成功"); Console.ReadKey(); }