如何将table里面的所有内容导入到excel里面,保留内容,页面格式,除去控件!!

解决方案 »

  1.   

    如果导出的excel只是用来查看,那么可以很容易就将table导出到excel中(这时候excel是非标准的,绝对不可以再用于导入)
    用Response输出,把文件MEMI改成excel的(具体值我忘了)注意一下encoding,免得中文出乱码
      

  2.   

    至于去除控件,那只能根据你具体的需求,或者是写正则(这是最好的办法了)或者其它的匹配方式进行页面Html数据的匹配,然后剔除
      

  3.   

    用数据连接的方式打开Excel,把数据存入就行了,不过格式不好办,可以先建一个模版excel文件,这样就可以先把格式调好。
      

  4.   

    遍历TABLE,将有用的数据重新组成一个TABLE的string,输出string
    Response.ContentType = "application/vnd.ms-excel";
                    Response.AddHeader("Content-Disposition", "attachment;filename=yinshuashengchanjihuabiao.xls");
                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
                    Response.Write("<table border='1'>");
                    Response.Write("<tr>");
    Response.Write("</tr>");
                    Response.Write("</table>");
                    Response.End();
      

  5.   

       #region 将DataTable的数据导出显示为报表
            /// <summary>
            /// 将DataTable的数据导出显示为报表
            /// </summary>
            /// <param name="dt">要导出的数据</param>
            /// <param name="strTitle">导出报表的标题</param>
            /// <param name="FilePath">保存文件的路径</param>
            /// <returns></returns>
            public string OutputExcel(System.Data.DataTable dt, string strTitle, string FilePath)
            {
                beforeTime = DateTime.Now;            Excel.Application excel;
                Excel._Workbook xBk;
                Excel._Worksheet xSt;            int rowIndex = 4;
                int colIndex = 1;            excel = new Excel.ApplicationClass();
                xBk = excel.Workbooks.Add(true);
                xSt = (Excel._Worksheet)xBk.ActiveSheet;            //取得列标题
                foreach (DataColumn col in dt.Columns)
                {
                    colIndex++;
                    excel.Cells[4, colIndex] = col.ColumnName;                //设置标题格式为居中对齐
                    xSt.get_Range(excel.Cells[4, colIndex], excel.Cells[4, colIndex]).Font.Bold = true;
                    xSt.get_Range(excel.Cells[4, colIndex], excel.Cells[4, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
                    xSt.get_Range(excel.Cells[4, colIndex], excel.Cells[4, colIndex]).Select();
                    xSt.get_Range(excel.Cells[4, colIndex], excel.Cells[4, colIndex]).Interior.ColorIndex = LTP.Common.ConfigHelper.GetConfigInt("ColorIndex");//19;//设置为浅黄色,共计有56种
                }
                //取得表格中的数据
                foreach (DataRow row in dt.Rows)
                {
                    rowIndex++;
                    colIndex = 1;
                    foreach (DataColumn col in dt.Columns)
                    {
                        colIndex++;
                        if (col.DataType == System.Type.GetType("System.DateTime"))
                        {
                            excel.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
                            xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
                        }
                        else
                            if (col.DataType == System.Type.GetType("System.String"))
                            {
                                excel.Cells[rowIndex, colIndex] = "'" + row[col.ColumnName].ToString();
                                xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
                            }
                            else
                            {
                                excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
                            }
                    }
                }            //加载一个合计行
                int rowSum = rowIndex + 1;
                int colSum = 2;
                excel.Cells[rowSum, 2] = "合计";
                xSt.get_Range(excel.Cells[rowSum, 2], excel.Cells[rowSum, 2]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                //设置选中的部分的颜色
                xSt.get_Range(excel.Cells[rowSum, colSum], excel.Cells[rowSum, colIndex]).Select();
                //xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex =Assistant.GetConfigInt("ColorIndex");// 1;//设置为浅黄色,共计有56种            //取得整个报表的标题
                excel.Cells[2, 2] = strTitle;            //设置整个报表的标题格式
                xSt.get_Range(excel.Cells[2, 2], excel.Cells[2, 2]).Font.Bold = true;
                xSt.get_Range(excel.Cells[2, 2], excel.Cells[2, 2]).Font.Size = 22;            //设置报表表格为最适应宽度
                xSt.get_Range(excel.Cells[4, 2], excel.Cells[rowSum, colIndex]).Select();
                xSt.get_Range(excel.Cells[4, 2], excel.Cells[rowSum, colIndex]).Columns.AutoFit();            //设置整个报表的标题为跨列居中
                xSt.get_Range(excel.Cells[2, 2], excel.Cells[2, colIndex]).Select();
                xSt.get_Range(excel.Cells[2, 2], excel.Cells[2, colIndex]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenterAcrossSelection;            //绘制边框
                xSt.get_Range(excel.Cells[4, 2], excel.Cells[rowSum, colIndex]).Borders.LineStyle = 1;
                xSt.get_Range(excel.Cells[4, 2], excel.Cells[rowSum, 2]).Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThick;//设置左边线加粗
                xSt.get_Range(excel.Cells[4, 2], excel.Cells[4, colIndex]).Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThick;//设置上边线加粗
                xSt.get_Range(excel.Cells[4, colIndex], excel.Cells[rowSum, colIndex]).Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThick;//设置右边线加粗
                xSt.get_Range(excel.Cells[rowSum, 2], excel.Cells[rowSum, colIndex]).Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThick;//设置下边线加粗            afterTime = DateTime.Now;            //显示效果
                //excel.Visible=true;
                //excel.Sheets[0] = "sss";            ClearFile(FilePath);
                string filename = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xls";
                excel.ActiveWorkbook.SaveAs(FilePath + filename, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);            //wkbNew.SaveAs strBookName;
                //excel.Save(strExcelFileName);            #region  结束Excel进程            //需要对Excel的DCOM对象进行配置:dcomcnfg
                //excel.Quit();
                //excel=null;                        xBk.Close(null, null, null);
                excel.Workbooks.Close();
                excel.Quit();
                //注意:这里用到的所有Excel对象都要执行这个操作,否则结束不了Excel进程
                // if(rng != null)
                // {
                // System.Runtime.InteropServices.Marshal.ReleaseComObject(rng);
                // rng = null;
                // }
                // if(tb != null)
                // {
                // System.Runtime.InteropServices.Marshal.ReleaseComObject(tb);
                // tb = null;
                // }
                if (xSt != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
                    xSt = null;
                }
                if (xBk != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
                    xBk = null;
                }
                if (excel != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
                    excel = null;
                }
                GC.Collect();//垃圾回收
                #endregion            return filename;        }
            #endregion