• 文件上传下下载(不包含断点续传) Excel,Word导入导出基础


    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:UsersAdministratorDesktop自己练习上传下载UpLoadUpLoadPictureQQ图片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:UsersadminDesktopuser.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+"	");
                    }
                }
                Console.ReadKey();
            }

    3、提取数据库数据到Excel(控制台应用)

    //提取数据到Excal
            static void Main(string[] args)
            {
                //读取证书
                License license = new License();
                license.SetLicense(@"C:UsersadminDesktop自己练习表达式树从数据库中读取数据到ExcalAidLicense.lic");
                DataTable dt = GetDataTable();
                //全新的工作簿
                Workbook wb = new Workbook();
                //工作表
                Worksheet ws = wb.Worksheets[0];
                ws.Cells.ImportDataTable(dt,true,"A1");
                wb.Save(@"C:UsersadminDesktop\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:UsersAdministratorDesktop新建 Microsoft Word 文档"))
                {
                    Console.WriteLine("文件已存在");
                }
                FileStream fs = new FileStream(@"C:UsersAdministratorDesktop新建 Microsoft Word 文档", FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine("床前明月光");
                sw.WriteLine("疑是地上霜");
                sw.Close();
                fs.Close();
                Console.WriteLine("写入成功");
                Console.ReadKey();
            }
  • 相关阅读:
    字符串转换整数 (atoi)
    Z 字形变换
    最长回文子串
    寻找两个有序数组的中位数
    二维码QRCode
    多个线程访问url
    store procedure 翻页
    store procedure example
    使用graphics2D给图片上画字符
    procedure的over(partition by ) function
  • 原文地址:https://www.cnblogs.com/dujian123/p/10582695.html
Copyright © 2020-2023  润新知