lijinchang

导航

今天写的一个导出html页面的过程

     private string ExportHtml()
        {
            string msg = "";
            try
            {
                #region
                string fileName = Application.StartupPath + "\\模板\\template.htm";//模板路径
                DataTable tb = SqlModel.Query("SELECT A.*,B.DD0022_002,ISNULL(X.DD0022_002,'')AS 股票代码,ISNULL(X.DD0022_002,'')AS 债券代码,ISNULL(Y.DD0022_002,'')AS 港股 FROM(SELECT DISTINCT A.NEWSCODE AS 新闻编码,A.NW0001_002 AS 文章标题," +
                "A.NW0001_003 AS 文章正文,A.NW0001_004 AS 媒体来源,B.ITCODE AS ITCODE,C.ITNAME AS ITNAME " +
                "FROM CMBDB.DBO.CSNW0003 A INNER JOIN(SELECT NEWSCODE,INDICATORSCODE,ITCODE FROM CMBDB.DBO.CSNW0002 )B  " +
                "ON A.NEWSCODE = B.NEWSCODE LEFT JOIN(SELECT * FROM DD0006) C ON B.ITCODE = C.ITCODE WHERE B.INDICATORSCODE<>'90101' AND A.NW0001_001='" + DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00.000' AND A.ENTRYTIME<='09:00:00' " +
                ")A LEFT JOIN(SELECT SENAME,DD0022_002,ITCODE FROM(SELECT SENAME,DD0022_002,ITCODE,ROW_NUMBER()OVER(PARTITION BY ITCODE ORDER BY DD0022_002 ASC) AS RANK FROM " +
                "(SELECT SECODE,SENAME FROM BASICDB.DBO.DD0007 WHERE DD0007_002='1' AND DD0007_004 IN (101,102) )A LEFT JOIN " +
                "DD0011 B ON A.SECODE=B.SECODE LEFT JOIN DD0009 C " +
                "ON B.SECODE=C.SECODE LEFT JOIN DD0022 D ON C.TRCODE=D.SECODE WHERE DD0022_001 ='5000' )Z WHERE RANK=1)B ON A.ITCODE=B.ITCODE LEFT JOIN(SELECT ITCODE,DD0022_002 FROM(SELECT * FROM DD0011) A " +
                "LEFT JOIN DD0009 B ON A.SECODE=B.SECODE LEFT JOIN DD0022 C ON B.TRCODE=C.SECODE WHERE A.SECODE LIKE '104%' AND DD0022_001='5000' AND MKCODE IN (1,2))X ON X.ITCODE=B.ITCODE " +
                "LEFT JOIN(SELECT ITCODE,DD0022_002 FROM(SELECT * FROM DD0011 WHERE LEFT(SECODE,3)='101')A LEFT JOIN(SELECT * FROM DD0009 WHERE LEFT(TRCODE,3)='201')B ON A.SECODE=B.SECODE " +
                "LEFT JOIN(SELECT * FROM DD0022 WHERE DD0022_001='5000' AND MKCODE='3')C ON B.TRCODE=C.SECODE)Y ON Y.ITCODE=A.ITCODE");//类型1的信息
                string html = string.Empty;//所有的html;
                string titlelink1 = string.Empty;//上市公司及债券人标题链接
                string ArticleContent1 = string.Empty;//上市公司及债券人内容
                string titlelink2 = string.Empty;//其他机构标题链接
                string ArticleContent2 = string.Empty;//其他机构内容
                string titlelink3=string.Empty;//行业标题连接
                string ArticleContent3=string.Empty;//行业内容
                string ArticleContent33=string.Empty;//行业内容
                string code = "";
                string reasset = "";
                string sort = "";
                string content = "";
                int itcount = 0;
                int newscount = 0;
                Hashtable ht = new Hashtable();
                #endregion

                #region
                itcount = tb.Rows.Count;
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    if (tb.Rows[i]["DD0022_002"].ToString() == "" && tb.Rows[i]["港股"].ToString() == "")
                        continue;
                    code = tb.Rows[i][0].ToString();
                    if (ht.Contains(code))
                    {
                        continue;
                    }
                    else
                    {
                        ht.Add(code, i);
                    }
                    newscount += 1;
                    titlelink1 += "<li class='li'><a  class='ctitle' href='#a" + newscount + "'>" + tb.Rows[i][1].ToString() + "</a></li>";
                    ArticleContent1 += @"<table width='100%' border='0' align='center' cellpadding='0' cellspacing='0'  style='margin-top:0px;border-bottom:#CCC solid 1px;'>
                                      <tr>
                                        <td  colspan='3'>
                                       <div class='ArticleTitle'>
                                          <div style='margin:10px auto 1px;'><a name='a" + newscount + "'></a>" + tb.Rows[i][1].ToString() + @"</div>
                                                      <div style='margin:0px; font-size:12px; color:#404040; font-weight:normal;'>来源:" +tb.Rows[i][3].ToString()+@"
                                                  </div>
                                                  <div style='background:url(images/line.gif) no-repeat; width:749px; height:1px;'></div>
                                              
                                          </div>
                                         </td></tr>
                                      <tr>
                                        <td  colspan='3'class='paragraph'>"+tb.Rows[i][2].ToString()+@"
                                    </td>
                                        </tr>
                                      <tr>
                                        <td width='187'>&nbsp;</td>
                                        <td width='187'>&nbsp;</td>
                                        <td width='382' align='right'><a href='#_Top'><img src='images/top.gif' width='48' height='17'></a></td>
                                      </tr>
                                    </table>";
                }
                StreamReader sr = new StreamReader(fileName, System.Text.Encoding.GetEncoding("utf-8"));
                html = sr.ReadToEnd();
                html=html.Replace("<!--上市公司及债券人link-->", titlelink1);
                html=html.Replace("<!--上市公司及债券人Content-->", ArticleContent1);
                string path = Application.StartupPath + "\\temp\\负面信息监控早报" + DateTime.Now.ToString("yyyyMMdd") + ".htm";
                #endregion
                #region
                itcount = tb.Rows.Count;
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    if (tb.Rows[i]["DD0022_002"].ToString() != "" || tb.Rows[i]["港股"].ToString() != "")
                        continue;
                    code = tb.Rows[i][0].ToString();
                    if (ht.Contains(code))
                    {
                        continue;
                    }
                    else
                    {
                        ht.Add(code, i);
                    }
                    newscount += 1;
                    titlelink2 += "<li class='li'><a  class='ctitle' href='#a" + newscount + "'>" + tb.Rows[i][1].ToString() + "</a></li>";
                    ArticleContent2 += @"<table width='100%' border='0' align='center' cellpadding='0' cellspacing='0'  style='margin-top:0px;border-bottom:#CCC solid 1px;'>
                                      <tr>
                                        <td  colspan='3'>
                                       <div class='ArticleTitle'>
                                          <div style='margin:10px auto 1px;'><a name='a" + newscount + "'></a>" + tb.Rows[i][1].ToString() + @"</div>
                                                      <div style='margin:0px; font-size:12px; color:#404040; font-weight:normal;'>来源:" + tb.Rows[i][3].ToString() + @"
                                                  </div>
                                                  <div style='background:url(images/line.gif) no-repeat; width:749px; height:1px;'></div>
                                              
                                          </div>
                                         </td></tr>
                                      <tr>
                                        <td  colspan='3'class='paragraph'>" + tb.Rows[i][2].ToString() + @"
                                    </td>
                                        </tr>
                                      <tr>
                                        <td width='187'>&nbsp;</td>
                                        <td width='187'>&nbsp;</td>
                                        <td width='382' align='right'><a href='#_Top'><img src='images/top.gif' width='48' height='17'></a></td>
                                      </tr>
                                    </table>";
                }
                html=html.Replace("<!--其他机构link-->", titlelink2);
                html=html.Replace("<!--其他机构Content-->", ArticleContent2);
               
                #endregion
                #region 行业

                string hysql = @"select distinct
                                     a.NewsCode as 新闻编码,
                                        a.NW0001_002 as 文章标题,
                                     a.NW0001_003 as 文章正文,
                                        a.NW0001_004 as 媒体来源,
                                        DD0003_006,
                                    case when DD0003_006='地区分类' then REGIONNAME
                                       when DD0003_006='国标行业分类' then c.industryname
                                     end as abc,
                                        g.industryname,
                                     a.NW0001_001 as 发布时间,
                                     a.ENTRYTIME as 时间
                                     from
                                     CMBDB.DBO.csnw0003 A
                                     inner join
                                     (select NewsCode, NW0006_001,NW0006_002 from CMBDB.DBO.csnw0006
                                     where NW0006_001 not in (2)
                                     )B
                                        on a.newscode = b.newscode
                                     left join
                                     (
                                     select industrycode,industryname from basicdb.dbo.dd0025 where dd0025_003='9' and dd0025_004='1'
                                     union all
                                        select '254545690','电力、燃气及水的生产和供应业'
                                        union all
                                     select a.industrycode,b.industryname from
                                     (select industrycode,industryname,dd0025_004,industrycode2 from basicdb.dbo.dd0025 where dd0025_003='9' and dd0025_004='2' and industrycode<>'254545690')a
                                     left join
                                     (select industrycode,industryname,dd0025_004,industrycode2 from basicdb.dbo.dd0025 where dd0025_003='9' and dd0025_004='1')b
                                     on a.industrycode2=b.industrycode
                                     union all
                                     select a.industrycode,c.industryname from
                                     (select industrycode,industryname,dd0025_004,industrycode2 from basicdb.dbo.dd0025 where dd0025_003='9' and dd0025_004='3' and industrycode not in ('254545637','254545622'))a
                                     left join
                                     (select industrycode,industryname,dd0025_004,industrycode2 from basicdb.dbo.dd0025 where dd0025_003='9' and dd0025_004='2')b
                                     on a.industrycode2=b.industrycode
                                     left join
                                     (select industrycode,industryname,dd0025_004,industrycode2 from basicdb.dbo.dd0025 where dd0025_003='9' and dd0025_004='1')c
                                     on c.industrycode=b.industrycode2
                                     ) c
                                     on b.NW0006_002 = cast(c.industrycode as varchar(max))
                                     LEFT JOIN
                                     (SELECT * FROM CMBDB.DBO.DD0003 WHERE DD0003_001 = 152 AND DD0003_005 IN ('1','3'))D
                                     ON D.DD0003_005=B.NW0006_001
                                     LEFT JOIN
                                     (SELECT * FROM CMBDB.DBO.CSNW0006 WHERE NW0006_001='1')E
                                     ON E.NEWSCODE=A.NEWSCODE
                                     LEFT JOIN
                                     (SELECT * FROM basicdb.dbo.DD0020)F
                                     ON E.nw0006_002=F.REGIONCODE
                                         left join
                                     (select * from basicdb.dbo.dd0025) g
                                     on b.NW0006_002 = g.industrycode
                                     where a.NW0001_001='" + DateTime.Now.ToString("yyyy-MM-dd") + @" 00:00:00.000' and a.ENTRYTIME<='09:00:00' and a.examine='1' ---and nw0006_001=3
                                     and DD0003_006 is not null and DD0003_006<>'地区分类'";//类型3的信息

                tb = SqlModel.Query(hysql);
                List<string> listhyfl = new List<string>();
                foreach (DataRow drhyfl in tb.Rows)//检索出所有的行业大类放到一个list集合中
                {
                    if (!listhyfl.Contains(drhyfl["abc"].ToString()))
                        listhyfl.Add(drhyfl["abc"].ToString());
                }
                ht.Clear();
                int icount = 0;
                foreach (string hyfl in listhyfl)//新修改按行业分类输出、行业类型作为二级、标题为三级--ljc
                {
                    icount += 1;
                    ArticleContent33="";
                    titlelink3+="<div style='margin:2px 4px 0px;'><a class='ctype' href='#toc001'>&nbsp;&nbsp;【"+hyfl+"】</a></div><ul style='color: #CCC; margin:0px auto 0px;'>";
                    ArticleContent3+=@" <tr>
    <td width='5'></td>
    <td height='5' colspan='2'>
    <div class='"+(icount==1?"typehead":"typehead1")+@"'></div>
    </td>
    </tr><tr>
    <td style='padding:0px;'><img src='images/ProductType1.gif' width='5' height='40' border='0'/></td>
    <td width='30%' style='white-space:nowrap;'><div style='background:url(images/ProductType2.gif) repeat-x; width:auto; color:#FFF; font-family:Arial, Helvetica, sans-serif 14px; height:38px; width:50px; padding-top:8px;'>&nbsp;&nbsp;" + hyfl + @"&nbsp;&nbsp;</div></td>
    <td width='65%' style='border-right:#CCC solid 1px;'>&nbsp;&nbsp;</td>
  </tr>
  <tr>
    <td></td>
    <td colspan='2'>
     <div class='ArticleContent1'>
  <!--Content3-->
</div>
</td>
  </tr>
 ";
                    DataRow[] drSelect = tb.Select("abc='" + hyfl + "'");
                    for (int i = 0; i < drSelect.Length; i++)
                    {
                        code = drSelect[i][0].ToString();
                        //if (ht.Contains(code))
                        //{
                        //    continue;
                        //}
                        //else
                        //{
                        //    ht.Add(code, i);
                        //}
                        sort = drSelect[i][4].ToString();
                        newscount += 1;
                        if (sort == "国标行业分类")
                        {
                            reasset = drSelect[i]["industryname"].ToString();
                            sort = "国内-行业";
                        }
                        else if (sort == "地区分类")
                        {
                            if (drSelect[i]["industryname"].ToString() == "香港")
                            {
                                sort = "香港-行业";
                            }
                            else
                            {
                                sort = "国内-行业";
                            }
                            reasset = drSelect[i][5].ToString();
                        }
                         titlelink3+="<li class='li'><a  class='ctitle' href='#a"+newscount+"'>"+drSelect[i][1].ToString()+"</a></li>";
                         ArticleContent33 += @"<table width='100%' border='0' align='center' cellpadding='0' cellspacing='0'  style='margin-top:0px;border-bottom:#CCC solid 1px;'>
                                      <tr>
                                        <td  colspan='3'>
                                       <div class='ArticleTitle1'>
                                          <div style='margin:10px auto 1px;'><a name='a" + newscount + "'></a>" + drSelect[i][1].ToString() + @"</div>
                                                     <div style='margin:0px; font-size:12px; color:#404040; font-weight:normal;'>来源:" + drSelect[i][3].ToString() + "&nbsp;&nbsp;分类:" + sort + "&nbsp;&nbsp;关联资产:" + reasset + @"
                                                  </div>
                                                  <div style='background:url(images/line.gif) no-repeat; width:749px; height:1px;'></div>
                                              
                                          </div>
                                         </td></tr>
                                      <tr>
                                        <td  colspan='3' class='paragraph'>" + drSelect[i][2].ToString() + @"</td>
                                        </tr>
                                      <tr>
                                        <td width='187'>&nbsp;</td>
                                        <td width='187'>&nbsp;</td>
                                        <td width='382' align='right'><a href='#_Top'><img src='images/top.gif' width='48' height='17'></a></td>
                                      </tr>
                                    </table>";

                    }
                    titlelink3 += "</ul>";
                    ArticleContent3 = ArticleContent3.Replace(" <!--Content3-->", ArticleContent33);
                }
                html=html.Replace("<!--行业link-->", titlelink3);
                html=html.Replace("<!--行业Content-->", ArticleContent3);
                html = html.Replace("<a name=\"_Top\"></a><span class=\"date\" >2011-07-28", "<a name=\"_Top\"></a><span class=\"date\" >"+DateTime.Now.ToString("yyyy年MM月dd日"));
                path = Application.StartupPath + "\\temp\\负面信息监控早报" + DateTime.Now.ToString("yyyyMMdd") + ".htm";
                if (!File.Exists(path))
                {
                    using (StreamWriter sw = File.CreateText(path))
                    {
                        sw.Write(html);
                    }
                }
                else
                {
                    FileStream fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    using (StreamWriter sw =new StreamWriter(fs))
                    {
                        sw.Write(html);
                    }

                }
                //newDbInput.Mail.Preview review = new newDbInput.Mail.Preview(html);
                //review.Show();
               #endregion
                msg = "导出成功!";
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return msg;
        }

    }

posted on 2011-07-27 16:12  lijinchang  阅读(543)  评论(0编辑  收藏  举报