Asp.Net 中 动态生成 Excel
2012-01-31 17:25
423 查看
1,Server 端存在 Excel 文件,也就是模板。 复制模板,取得要显示的数据,以操作表的形式,将数据写入Excel
。关于 操作方法 原来的Post上面有,在此不详细说明了。
2,Server端无模板,通过 xslt 和
xml 转化成 Excel 文件,xslt 中 是要求的Excel文件显示的式样(style),xml 中当然是数据啦。
下面是一段主要的代码
//动态生成 xslt 文件 strXSLTPath 是要保存的 xslt 文件 在 Server上的绝对路径
System.IO.StreamWriter sw = new System.IO.StreamWriter(strXSLTPath);
sw.WriteLine("<xsl:stylesheet version=\"1.0\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"");
sw.WriteLine(" xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\" xmlns:user=\"urn:my-scripts\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
sw.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">");
sw.WriteLine(" <xsl:template match=\"NewDataSet\">");
sw.WriteLine(" <Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
sw.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sw.WriteLine(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
sw.WriteLine(" <Styles>");
sw.WriteLine(" <Style ss:ID=\"Default\" ss:Name=\"Normal\">");
sw.WriteLine(" <Alignment ss:Vertical=\"Bottom\" />");
sw.WriteLine(" <Borders />");
sw.WriteLine(" <Font ss:FontName=\"MS Pゴシック\" x:CharSet=\"128\" x:Family=\"Modern\" ss:Size=\"11\" />");
sw.WriteLine(" <Interior />");
sw.WriteLine(" <NumberFormat />");
sw.WriteLine(" <Protection />");
sw.WriteLine(" </Style>");
sw.WriteLine(" </Styles>");
sw.WriteLine(" <Worksheet ss:Name=\"DataSheet\">");
sw.WriteLine(" <Table ss:ExpandedColumnCount=\"" + intColCount + "\" ss:ExpandedRowCount=\"" + (intRowCount + 1) + "\" x:FullColumns=\"1\" x:FullRows=\"1\"");
sw.WriteLine(" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"13.5\">");
sw.WriteLine(" <xsl:apply-templates select=\"Fields\" />");
sw.WriteLine(" <xsl:apply-templates select=\"SearchRslts\" />");
sw.WriteLine(" </Table>");
sw.WriteLine(" </Worksheet>");
sw.WriteLine(" </Workbook>");
sw.WriteLine(" </xsl:template>");
sw.WriteLine(" <xsl:template match=\"Fields\">");
sw.WriteLine(" <Row>");
for(i=0;i<intColCount;i++)
{
sw.WriteLine(" <Cell>");
sw.WriteLine(" <Data ss:Type=\"String\">");
sw.WriteLine(" <xsl:value-of select=\"field" + (i+1) +"\" />");
sw.WriteLine(" </Data>");
sw.WriteLine(" </Cell>");
}
sw.WriteLine(" </Row>");
sw.WriteLine(" </xsl:template>");
sw.WriteLine(" <xsl:template match=\"SearchRslts\">");
sw.WriteLine(" <Row>");
for(i=0;i<intColCount;i++)
{
sw.WriteLine(" <Cell>");
sw.WriteLine(" <Data ss:Type=\"String\">");
sw.WriteLine(" <xsl:value-of select=\"" + dsData.Tables[0].Columns[i].ColumnName + "\" />");
sw.WriteLine(" </Data>");
sw.WriteLine(" </Cell>");
}
sw.WriteLine(" </Row>");
sw.WriteLine(" </xsl:template>");
sw.WriteLine("</xsl:stylesheet>");
sw.Close();
//从DataSet 生成 XML
dsData 是 Dataset 对象
//First, save the XML representation of the DataSet in a file
//and add a processing instruction to the XML so that it can be
//transformed client-side.
System.Xml.XmlTextWriter tw;
System.Random randa = new System.Random();
string strXMLPath = Server.MapPath(randa.Next() + ".xml");
tw = new System.Xml.XmlTextWriter(strXMLPath, System.Text.Encoding.UTF8);
tw.Formatting = System.Xml.Formatting.Indented;
tw.Indentation = 3;
tw.WriteStartDocument();
tw.WriteProcessingInstruction("xml-stylesheet",
"type='text/xsl' href=""+" target="_blank" rel="nofollow"> dsData.WriteXml(tw);
tw.Close();
// 转换成 excel 文件
//Second, transform the DataSet XML and save it to a file.
System.Xml.XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument(dsData);
System.Xml.XPath.XPathNavigator nav = xmlDoc.CreateNavigator();
System.Xml.Xsl.XslTransform xslTran = new System.Xml.Xsl.XslTransform();
xslTran.Load(strXSLTPath);
tw = new System.Xml.XmlTextWriter(SaveAs, System.Text.Encoding.UTF8);
tw.Formatting = System.Xml.Formatting.Indented;
tw.Indentation = 3;
tw.WriteStartDocument();
xslTran.Transform(nav,null,tw,null);
tw.Close();
//Delete the XML File
System.IO.File.Delete(strXMLPath);
。关于 操作方法 原来的Post上面有,在此不详细说明了。
2,Server端无模板,通过 xslt 和
xml 转化成 Excel 文件,xslt 中 是要求的Excel文件显示的式样(style),xml 中当然是数据啦。
下面是一段主要的代码
//动态生成 xslt 文件 strXSLTPath 是要保存的 xslt 文件 在 Server上的绝对路径
System.IO.StreamWriter sw = new System.IO.StreamWriter(strXSLTPath);
sw.WriteLine("<xsl:stylesheet version=\"1.0\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"");
sw.WriteLine(" xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\" xmlns:user=\"urn:my-scripts\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
sw.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">");
sw.WriteLine(" <xsl:template match=\"NewDataSet\">");
sw.WriteLine(" <Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
sw.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sw.WriteLine(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
sw.WriteLine(" <Styles>");
sw.WriteLine(" <Style ss:ID=\"Default\" ss:Name=\"Normal\">");
sw.WriteLine(" <Alignment ss:Vertical=\"Bottom\" />");
sw.WriteLine(" <Borders />");
sw.WriteLine(" <Font ss:FontName=\"MS Pゴシック\" x:CharSet=\"128\" x:Family=\"Modern\" ss:Size=\"11\" />");
sw.WriteLine(" <Interior />");
sw.WriteLine(" <NumberFormat />");
sw.WriteLine(" <Protection />");
sw.WriteLine(" </Style>");
sw.WriteLine(" </Styles>");
sw.WriteLine(" <Worksheet ss:Name=\"DataSheet\">");
sw.WriteLine(" <Table ss:ExpandedColumnCount=\"" + intColCount + "\" ss:ExpandedRowCount=\"" + (intRowCount + 1) + "\" x:FullColumns=\"1\" x:FullRows=\"1\"");
sw.WriteLine(" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"13.5\">");
sw.WriteLine(" <xsl:apply-templates select=\"Fields\" />");
sw.WriteLine(" <xsl:apply-templates select=\"SearchRslts\" />");
sw.WriteLine(" </Table>");
sw.WriteLine(" </Worksheet>");
sw.WriteLine(" </Workbook>");
sw.WriteLine(" </xsl:template>");
sw.WriteLine(" <xsl:template match=\"Fields\">");
sw.WriteLine(" <Row>");
for(i=0;i<intColCount;i++)
{
sw.WriteLine(" <Cell>");
sw.WriteLine(" <Data ss:Type=\"String\">");
sw.WriteLine(" <xsl:value-of select=\"field" + (i+1) +"\" />");
sw.WriteLine(" </Data>");
sw.WriteLine(" </Cell>");
}
sw.WriteLine(" </Row>");
sw.WriteLine(" </xsl:template>");
sw.WriteLine(" <xsl:template match=\"SearchRslts\">");
sw.WriteLine(" <Row>");
for(i=0;i<intColCount;i++)
{
sw.WriteLine(" <Cell>");
sw.WriteLine(" <Data ss:Type=\"String\">");
sw.WriteLine(" <xsl:value-of select=\"" + dsData.Tables[0].Columns[i].ColumnName + "\" />");
sw.WriteLine(" </Data>");
sw.WriteLine(" </Cell>");
}
sw.WriteLine(" </Row>");
sw.WriteLine(" </xsl:template>");
sw.WriteLine("</xsl:stylesheet>");
sw.Close();
//从DataSet 生成 XML
dsData 是 Dataset 对象
//First, save the XML representation of the DataSet in a file
//and add a processing instruction to the XML so that it can be
//transformed client-side.
System.Xml.XmlTextWriter tw;
System.Random randa = new System.Random();
string strXMLPath = Server.MapPath(randa.Next() + ".xml");
tw = new System.Xml.XmlTextWriter(strXMLPath, System.Text.Encoding.UTF8);
tw.Formatting = System.Xml.Formatting.Indented;
tw.Indentation = 3;
tw.WriteStartDocument();
tw.WriteProcessingInstruction("xml-stylesheet",
"type='text/xsl' href=""+" target="_blank" rel="nofollow"> dsData.WriteXml(tw);
tw.Close();
// 转换成 excel 文件
//Second, transform the DataSet XML and save it to a file.
System.Xml.XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument(dsData);
System.Xml.XPath.XPathNavigator nav = xmlDoc.CreateNavigator();
System.Xml.Xsl.XslTransform xslTran = new System.Xml.Xsl.XslTransform();
xslTran.Load(strXSLTPath);
tw = new System.Xml.XmlTextWriter(SaveAs, System.Text.Encoding.UTF8);
tw.Formatting = System.Xml.Formatting.Indented;
tw.Indentation = 3;
tw.WriteStartDocument();
xslTran.Transform(nav,null,tw,null);
tw.Close();
//Delete the XML File
System.IO.File.Delete(strXMLPath);
相关文章推荐
- Asp.Net 中 动态生成 Excel 文件的心得
- ASP.NET Excel导出(动态生成)解决方案
- ASP.NET动态生成静态页面(C#)
- ASP_NET动态生成html页面
- asp.net中利用XML描述文件和Excel模板相结合的方法生成Excel报表
- SolpartMenu的使用:(二)、在ASP.NET页面中使用SolpartMenu控件之动态的加载数据库中的数据来生成菜单
- asp.net动态生成验证码
- ASP.NET MVC 2生成动态表单的一种最简单的思路
- 利用UrlRewrite,asp.net动态生成htm页面
- asp.net动态生成txt文件并提供用户下载代码
- 用ASP.NET动态生成图像(转2)
- asp.net MVC中使用自定义HtmlForm控件动态生成表单
- 摘要:asp.net动态生成html页面,适用
- Asp.net 生成 导出 Excel 下载
- ASP.NET动态生成html页面
- asp.net动态生成控件及访问控件
- 在Asp.net用C#建立动态Excel
- 编程技巧 Asp.net动态生成html页面
- Asp.Net 动态生成验证码
- ASP.NET生成excel失败(异常来自 HRESULT:0x800A03EC )