首页 新闻 搜索 专区 学院

在mvc 中导出Excel 怎么让用户自己选择路径

0
悬赏园豆:10 [已解决问题] 解决于 2012-05-10 22:15

List<JSW_JHD> list = new List<JSW_JHD>();
            string[] ids = id.Split(',');
            for (int i = 0; i < ids.Length; i++)
            {
                //根据bh编号查询
                JSW_JHD emp = companyService.getalljsw_jhdbybh(ids[i].ToString());
                list.Add(emp);
            }
            Excel.Application app = new Microsoft.Office.Interop.Excel.Application();

            //设置是否打开Excel
            app.Visible = true;
            //创建工作簿(添加一张新的工作表簿)
            Excel.Workbook wb = app.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
            //创建列名
            app.Cells[1, 1] = "日期";
            app.Cells[1, 2] = "对方单位";
            app.Cells[1, 3] = "入库";
            app.Cells[1, 4] = "对方经办";
            app.Cells[1, 5] = "仓库";
            app.Cells[1, 6] = "入库单号";
            app.Cells[1, 7] = "我方经办";
            app.Cells[1, 8] = "到货时间";
            app.Cells[1, 9] = "运输方式";
            app.Cells[1, 10] = "物流公司";
            app.Cells[1, 11] = "审核人员";
            app.Cells[1, 12] = "审核时间";
            app.Cells[1, 13] = "审核级别";
            int j = 2;//行从第二行开始
            foreach (var item in list)
            {
                app.Cells[j, 1] = item.DJRQ;
                app.Cells[j, 2] = item.DFDW;
                app.Cells[j, 3] = item.RKQK;
                app.Cells[j, 4] = item.DFJB;
                app.Cells[j, 5] = item.CKBH;
                app.Cells[j, 6] = item.RKDH;
                app.Cells[j, 7] = item.WFJB;
                app.Cells[j, 8] = item.DHSJ;
                app.Cells[j, 9] = item.YSFS;
                app.Cells[j, 10] = item.WLGS;
                app.Cells[j, 11] = item.SHRY;
                app.Cells[j, 12] = item.SHSJ;
                app.Cells[j, 13] = item.XSJB;
                j++;//行加一
            }
            //保存修改
           
            //SaveFileDialog fileDlg = new SaveFileDialog();
            //  fileDlg.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
            //  fileDlg.FilterIndex = 1;
            //  fileDlg.RestoreDirectory = true;

            //  if (fileDlg.ShowDialog() == DialogResult.OK)
            //  {
            //  if(File.Exists(fileDlg.FileName))
            //  File.Delete(fileDlg.FileName);
            //引用excel工作薄

       //保存
            wb.SaveAs(string.Format(@"E:\调达计划信息表" + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second), Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            //释放资源
            wb = null;
            app.Quit();
            app = null;

            return "ok";

问题补充:

我是硬写在E盘的,这样不好。让用户自己选择路径 我是硬写在E:\调达计划信息表加时间的我是想让用户自己选择路径。怎么弹出个用户选择路径的框 在点击保存

NET技术员的主页 NET技术员 | 初学一级 | 园豆:8
提问于:2012-04-12 15:38
< >
分享
最佳答案
0

/// <summary>
        /// 选择保存htm文件的文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            folderBrowserDialog1.Description = "请选择保存htm文件的文件夹";
            //folderBrowserDialog1.ShowNewFolderButton = true;
            //folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Personal;
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string folderName = folderBrowserDialog1.SelectedPath;
                if (folderName != "")
                {
                    textBox1.Text = folderName;
                    SaveHtmPath_EXCEL = textBox1.Text;
                }
            }
   
        }

不知道这是不是你要的。

收获园豆:4
悟行 | 专家六级 |园豆:12422 | 2012-04-12 16:27
其他回答(4)
0

写ashx来截获Request请求,这个有个示例:
http://www.cnblogs.com/downmoon/archive/2011/05/30/2063258.html

收获园豆:3
邀月 | 园豆:25375 (高人七级) | 2012-04-12 15:55
0

您的这段代码 应该是Server端的代码吧,把Server上面的路径开放给Client端看的话,需要传送数据到Client端的javascript代码上啊, 而且也不够安全啊,服务器端是没法设置Client端的文件位置什么的。服务器上面的对话框什么的客户端是看不到的。

收获园豆:3
ChatinCode | 园豆:2272 (老鸟四级) | 2012-04-13 13:32
0

根本就不对

chinawangjian | 园豆:202 (菜鸟二级) | 2013-01-30 10:18
0

最佳答案那个也是醉了,人家问的是MVC,你整一个窗体应用程序的方法

kangkangkf | 园豆:202 (菜鸟二级) | 2015-06-12 14:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册