首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

——dataTable数据导出到Excel文件,如何实现选择保存路径

2012-12-15 
在线等——dataTable数据导出到Excel文件,怎么实现选择保存路径?实现一个SQL查询出来的数据导入到Excel文件,

在线等——dataTable数据导出到Excel文件,怎么实现选择保存路径?
实现一个SQL查询出来的数据导入到Excel文件,功能是实现,可是有个问题就是我的路径是写死的,怎么实现让用户选择保存路径 代码贴出如下:


  //数据导出
        private void DataTabletoExcel(System.Data.DataTable tmpDataTable, string strFileName)
        {
            if (tmpDataTable == null)
            {
                return;
            }
            int rowNum = tmpDataTable.Rows.Count;
            int columnNum = tmpDataTable.Columns.Count;
            int rowIndex = 1;
            int columnIndex = 0;
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            xlApp.DefaultFilePath = "D:\";
            xlApp.DisplayAlerts = true;
            xlApp.SheetsInNewWorkbook = 1;
            Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
            //将DataTable的列名导入Excel表第一行
            foreach (DataColumn dc in tmpDataTable.Columns)
            {
                columnIndex++;
                xlApp.Cells[rowIndex, columnIndex] = dc.ColumnName;
            }
            //将DataTable中的数据导入Excel中
            for (int i = 0; i < rowNum; i++)
            {
                rowIndex++;
                columnIndex = 0;
                for (int j = 0; j < columnNum; j++)
                {
                    columnIndex++;
                    xlApp.Cells[rowIndex, columnIndex] = tmpDataTable.Rows[i][j].ToString();
                }
            }


            xlBook.SaveCopyAs(strFileName + ".xls");

        }



   xlApp.DefaultFilePath = "D:\"; 这个是我的路径,写死了,求高手指点。网上搜的都是Winform代码
[最优解释]
用上面的方法,有个问题 GridView1.RenderControl(htmlWrite);
这个是将GridView1数据读取出来,
我现在的数据源是一个查询出来的数据表,或者保存好的Excel文件,
我要怎么操作才能把这个Excel文件的内容输出到htmlWrite中去,或者将Table中的内容输出到htmlWrite中去
[其他解释]
把 xlApp.DefaultFilePath = "D:\"; 注释掉
strFileName 传绝对路径进来.
xlBook.SaveCopyAs 改为 xlBook.SaveAs


[其他解释]
*.xls";
            //设置默认文件类型显示顺序
            saveFileDialog1.FilterIndex = 2;
            //保存对话框是否记忆上次打开的目录
            saveFileDialog1.RestoreDirectory = true;
            //点了保存按钮进入
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
               DataTable dt=(DataTable)this.DataGridView.DataSource;
               TableToExcel(saveFileDialog1.FileName, dt, "test");
            }
        }       


参考
[其他解释]
网络上搜下导出Excel文件的代码,可以弹出下载对话框,让你选择保存路径的。
[其他解释]
private void SaveFileDialog()
        {
            //string localFilePath, fileNameExt, newFileName, FilePath;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            //设置文件类型
            saveFileDialog1.Filter = " excel files(*.xls)
[其他解释]
    protected void btnPrint_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=Order.xls");
        Response.Charset = "gb2312";
        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);



        GridView1.AllowPaging = false;
        Bin();
        GridView1.RenderControl(htmlWrite);

        Response.Write(stringWrite.ToString());
        Response.End();
        GridView1.AllowPaging = true;
        Bin();
    }


或许对你有帮助
[其他解释]
引用:
C# code
private void SaveFileDialog()
        {
            //string localFilePath, fileNameExt, newFileName, FilePath;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        ……

+1
[其他解释]
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
这个怎么提示找不到命名空间,是ASP.NET里面的吗?
[其他解释]
SaveFileDialog  是winform的。

lz想怎么选文件夹呢?选完也是服务端的文件夹,客户端也看不到。
[其他解释]
asp.net客户端保存文件,得用Response.WriteFile

先在服务端生成临时的Excel文件 > 用Attachment方式写回Response

客户端会弹出是否保存的对话框,自己选择文件夹保存。
[其他解释]
不好意思啊,我说错了,意思是:我导出的Excel文件用户要能够下载到他本机上,下载到本机时可以选择路径
[其他解释]
好滴我试试

[其他解释]
我那种方法可以提供。下班,闪人~
[其他解释]
打开EXCEL模版赋值下载
FileInfo Fi = new FileInfo(filePath);
if (Fi.Exists)
{
  FileStream fs = new FileStream(filePath, FileMode.Open);
  byte[] bytes = new byte[(int)fs.Length];
  fs.Read(bytes, 0, bytes.Length);
  fs.Close();
  Response.ContentType = "application/octet-stream";
  Response.AddHeader("Content-Disposition", "attachment; filename=1.excel");
  Response.BinaryWrite(bytes);
  Response.Flush();
  Response.End();
}

[其他解释]
可以问下,你是怎么解决的吗,我现在也遇到这个问题。
[其他解释]
我想问下你这种方法不是占用内存严重吗
[其他解释]
我也想知道下。和楼主遇到同样地问题
[其他解释]
可以问下楼主该问题现在解决了吗?请赐教!拜托了!
[其他解释]
我也想请教啊,同样的问题无法实现……

热点排行