HtmlText To Excel

 

 

public static string GetHtmlText(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<style>");
            builder.Append("#chris{ font-family:Times New Roman;font-style:normal;;vertical-align:middle;}");
            builder.Append(".title1{ font-family:Arial;text-align:center;vertical-align:middle;font-size:22pt;}");
            builder.Append(".title2{ text-align:center;vertical-align:middle;font-size:14pt;height:45}");
            builder.Append(".title3{ font-family:Arial;text-align:center;vertical-align:middle;font-size:10pt;height:35}");
            builder.Append(".content{ font-family:Arial;text-align:center;font-size:10pt;background:yellow;}");
            builder.Append(".comment{ font-family:Arial;text-align:center;font-size:10pt;}");
            builder.Append("</style>");
            builder.Append("<table id=\"chris\" border=\"1\">");
            builder.Append("<tr><td colspan=\"15\" class=\"title1\"><strong>XXXXXXXXXXXXXXXXXX</strong></td></tr>");
            builder.Append("<tr class=\"title2\">");
            builder.Append("<td>xxx</td>");
            builder.Append("<td>xxx</td>");
            builder.Append("<td>xxx</td>");
            builder.Append("<td colspan=\"4\">xxx</td>");
            builder.Append("<td>xxx</td>");
            builder.Append("<td>xxx</td>");
            builder.Append("<td>xxx</td>");
            builder.Append("<td>xxx</td>");
            builder.Append("<td>xxx</td>");
            builder.Append("<td>xxx</td>");
            builder.Append("<td>xxx</td>");
            builder.Append("<td>xxx</td>");

            builder.Append("<tr class=\"title3\">");
            builder.Append("<td style=\"background:#CC99FF\">xxx</td>");
            builder.Append("<td style=\"background:#7FFF00\">xxx</td>");
            builder.Append("<td style=\"background:#33CCCC\">xxx</td>");
            builder.Append("<td>xxx</td>");
            builder.Append("<td>xxx</td>");
            builder.Append("<td>xxx</td>");
            builder.Append("<td>xxx</td>");
            builder.Append("<td></td>");
            builder.Append("<td></td>");
            builder.Append("<td></td>");
            builder.Append("<td></td>");
            builder.Append("<td></td>");
            builder.Append("<td></td>");
            builder.Append("<td></td>");
            builder.Append("<td></td>");
            builder.Append("</tr>");

            Log entity = null;

            new Report().CreateLog(id,out entity);

            builder.Append("<tr class=\"content\">");
            builder.Append(string.Format("<td align=\"left\">{0}</td>", entity.xxx));
            builder.Append(string.Format("<td align=\"left\">{0}</td>", entity.xxx));
            builder.Append(string.Format("<td align=\"left\">{0}</td>", entity.xxx));
            builder.Append(string.Format("<td align=\"left\">{0}</td>", entity.xxx));
            builder.Append(string.Format("<td align=\"left\">{0}</td>", entity.xxx));
            builder.Append(string.Format("<td align=\"right\">{0}</td>", entity.xxx));
            builder.Append(string.Format("<td align=\"right\">{0}</td>", entity.xxx));
            builder.Append(string.Format("<td align=\"left\">{0}</td>", entity.xxx));
            builder.Append(string.Format("<td align=\"right\">{0:M/d/yyyy}</td>", entity.xxx));
            builder.Append(string.Format("<td align=\"center\">{0}</td>", entity.xxx));
            builder.Append(string.Format("<td align=\"right\">{0:M/d/yyyy}</td>", entity.xxx));
            builder.Append(string.Format("<td align=\"right\">{0}</td>", entity.xxx));
            builder.Append(string.Format("<td align=\"right\">{0}</td>", entity.xxx));
            builder.Append(string.Format("<td align=\"right\">{0}</td>", entity.xxx));
            builder.Append(string.Format("<td align=\"left\">{0}</td>", entity.xxx));
            builder.Append("</tr>");

            builder.Append("</table>");

            return builder.ToString();
        }

 

public static void HtmlTextToExcel(string htmltext, HttpResponse response, string filename)
        {

            response.Clear();
            response.Buffer = true;
            response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
            response.ContentEncoding = System.Text.Encoding.Default;
            response.ContentType = "application/ms-excel";
            response.Write(htmltext);
            response.Flush();
            response.End();
        }

 

public static void ExportToZip(string fullname, HttpResponse response, string taskid)
{
FileStream fileStream = new FileStream(fullname, FileMode.Open);
long fileSize = fileStream.Length;
byte[] fileBuffer = new byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
fileStream.Close();
response.Clear();
response.Buffer = true;
response.Charset = "gb2321";
response.AppendHeader("Content-Disposition", "attachment;filename=" + taskid + ".zip");
response.ContentEncoding = System.Text.Encoding.UTF8;
response.ContentType = "application/zip";
response.AddHeader("Content-Length", fileSize.ToString());
response.BinaryWrite(fileBuffer);
response.Flush();
response.End();
}

posted @ 2012-03-06 17:27  Yu  阅读(285)  评论(0编辑  收藏  举报