类似上图,像把这样的页面导到excel中,目前使用的代码如下:if (DocumentType == "Excel")
        {
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".xls", Encoding.UTF8));
            HttpContext.Current.Response.ContentType = "application/ms-excel";
        }
        else if (DocumentType == "Word")
        {
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".doc", Encoding.UTF8));
            HttpContext.Current.Response.ContentType = "application/ms-word";
        }
        HttpContext.Current.Response.Charset = "GB2312";
        HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
        source.Page.EnableViewState = false;
        StringWriter writer = new StringWriter();
        HtmlTextWriter writer2 = new HtmlTextWriter(writer);
        source.RenderControl(writer2);
        HttpContext.Current.Response.Write(writer.ToString());
        HttpContext.Current.Response.End();
这代码并不能把商品的图片也导进去,请问大家有什么好的控件或方法可以实现吗?谢谢

解决方案 »

  1.   

    不用控件,直接用html图片代码,导出的excel本身就是html格式的,不信你导出后看源文件。
      

  2.   

    用office2007以上的格式,导出的就是html格式的office文件
      

  3.   

    <img src="/upload/newpro3.GIF" width="30px" height="30px" class="cartimg" />
    商品图片处有这样的代码,导出后还是一个叉叉,不显示图片。
      

  4.   

    把图片路径由相对转成绝对的,如:http://***/a.img不过不完美,这要求需要打开该Excel文件的机器能连接服务器如果文档内容具有一定格式,可以考虑用模板机制(Word中有)
      

  5.   

    是可以导出的,你可以按照你的格式先画好excel模板,然后用程序将模板复制一份,将数据导出到模板就可以了。我最近做了这个功能。给你一份。 /// <summary>
            /// 导出到excel模板
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnExcel_Click(object sender, EventArgs e)
            {
                //检查是否安装excel程序
                if (!codeboolisExcelInstalled())
                {
                    MessageBox.Show("当前系统没有发现可执行的Excel文件, 如需使用Excel功能请先安装office", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (this.dgvITList.Rows.Count - 1 == 0)
                {
                    MessageBox.Show("销售数据未填写,无法导出EXCEL文件!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                for (int colNumb = 0; colNumb < dgvITList.Rows.Count - 1; colNumb++)
                {
                    //判断是否有 数量、价格、金额合计没有填写完整的行
                    if (dgvITList.Rows[colNumb].Cells["Number"].Value == null || dgvITList.Rows[colNumb].Cells["colRealPrice"].Value == null || dgvITList.Rows[colNumb].Cells["Column17"].Value == null)
                    {
                        MessageBox.Show("销售数据未填写完整,请将数据填写完整!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
                //以顾客名称加上日期来命名要保存到的文件夹
                string Path1 = Application.StartupPath + "\\SaveExcel";
                string Path2 = Application.StartupPath + "\\ExcelModal";
                if (!Directory.Exists(Path1))//如果不存在就创建SaveExcel 文件夹
                {
                    Directory.CreateDirectory(Path1);
                }
                if (!Directory.Exists(Path2))//如果不存在就创建ExcelModal文件夹
                {
                    Directory.CreateDirectory(Path2);
                }            //判断模板文件是否存在?
                string path2;//源文件路径名称
                saveFileDialog1.FileName = cbxCustomer.Text + DateTime.Now.ToString("yyyymmdd") + "销售清单";
                saveFileDialog1.Title = "销售报表存放位置";
                saveFileDialog1.Filter = "excel files(*.xls)|*.xls";
                saveFileDialog1.FilterIndex = 0;
                //以顾客名称加上日期来命名要保存到的文件夹
                saveFileDialog1.InitialDirectory = Application.StartupPath + "\\SaveExcel";
                saveFileDialog1.RestoreDirectory = true;
                if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
                {
                    return;
                }
                path2 = saveFileDialog1.FileName;
                ExcelFileCopy(path2);//复制模板到用户指定路径            if (path2 == null)
                {
                    MessageBox.Show("Excel模板文件路径不能为空!", "系统信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    return;
                }
                if (!File.Exists(path2))
                {
                    MessageBox.Show("指定路径的Excel模板文件不存在!", "系统信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    return;
                }
                templetFile = path2;
                //C#创建Excel文件之取得数据  
                DataTable dtE = GetData();
                if (dtE != null)
                {
                    int rowCount = dtE.Rows.Count;
                    DataTableToExcel(dtE);
                }
                this.Cursor = Cursors.Default;
            }
            /// <summary> 
            /// 10进制到Excel的26进制的转换函数 
            /// </summary> 
            /// <param name="i"></param> 
            /// <returns></returns> 
            private string convertToCharacter(int i)
            {
                char[] list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
                StringBuilder sb = new StringBuilder();
                while ((i - 1) / 26 != 0)
                {
                    sb.Append(list[i / 26 - 1]);
                    i = i % 26;
                }
                i = (i - 1) % 26;
                sb.Append(list[i]);
                ColNum = sb.ToString();
                return ColNum;
            }
      

  6.   


     /// <summary>
            /// Excel模板复制add by gfl20110414
            /// </summary>
            /// <param name="path">源文件路径以及文件名称</param>
            /// <param name="path2">目标路径以及文件名称</param>
            /// <returns></returns>
            public void ExcelFileCopy(string path2)
            {
                string path = Application.StartupPath + @"\ExcelModal\销售清单.xls";
                FileInfo fi1 = new FileInfo(path);
                FileInfo fi2 = new FileInfo(path2);
                try
                {
                    if (fi2.Exists)
                    {
                        fi2.Delete();
                    }
                    fi1.CopyTo(path2);//复制模板到指定路径
                }
                catch
                {
                    Console.WriteLine("模板复制失败!");
                }
            }
            //判断本机是否安装Excel文件方法
            private bool codeboolisExcelInstalled()
            {
                Type type = Type.GetTypeFromProgID("Excel.Application");
                return type != null;
            }
            /// <summary>
            /// 获取数据
            /// </summary>
            /// <returns></returns>
            private DataTable GetData()
            {
                System.Data.DataTable dtExcel = new DataTable();
                //添加dtExcel列
                dtExcel.Columns.Add("名称", typeof(string));
                dtExcel.Columns.Add("件数", typeof(int));
                dtExcel.Columns.Add("数量", typeof(int));
                dtExcel.Columns.Add("单价", typeof(decimal));
                dtExcel.Columns.Add("金额", typeof(decimal));
                for (int colNum = 0; colNum < dgvITList.Rows.Count - 1; colNum++)
                {
                    DataRow dr = dtExcel.NewRow();
                    //将datagridview中某行某列的值添加到字段中
                    dr["名称"] = dgvITList.Rows[colNum].Cells["Column3"].Value;
                    if (dgvITList.Columns["Group1"].Visible == true && dgvITList.Rows[colNum].Cells["Group1"].Value != null)
                    {                    dr["件数"] = ComLibrary.ToInt(dgvITList.Rows[colNum].Cells["Group1"].Value);
                    }
                    else
                    {
                        dr["件数"] = 0;
                    }
                    dr["数量"] = dgvITList.Rows[colNum].Cells["Number"].Value.ToString().Replace(",", "");
                    dr["单价"] = dgvITList.Rows[colNum].Cells["colRealPrice"].Value.ToString().Replace(",", "");
                    dr["金额"] = dgvITList.Rows[colNum].Cells["Column17"].Value.ToString().Replace(",", "");
                    dtExcel.Rows.Add(dr);//将dgvITList的一行添加到dtExcel的行中
                }
                return dtExcel;
            }
      

  7.   

    谢谢gefangliang,但上面好像没有把图片也添加进去的功能。这是最主要的一个功能。