使用XML创建Excel文档

更新时间:2023-07-20 19:57:23 阅读: 评论:0

使用XML创建Excel文档     
原例子使用VB.Net写的,以下的用C#改写的代码
原文代码:
本例使用XML来创建Excel文档,但在运行时不需要安装Excel程序。
DataSet mDSData = new DataSet();
mDSData.Tables.Add("myTable");
英语六级考试流程
mDSData.Tables["myTable"].Columns.Add("ID");
mDSData.Tables["myTable"].Columns.Add("Name");
mDSData.Tables["myTable"].Columns.Add("PassWord");
for (int i = 0; i < 10; i++)
{
DataRow dr = mDSData.Tables["myTable"].NewRow();
dr["ID"] = i;
dr["Name"] = i;
dr["PassWord"] = i;
mDSData.Tables["myTable"].Rows.Add(dr);
}
SaveFileDialog dialog1 = new SaveFileDialog();
dialog1.AddExtension = true;
dialog1.CheckPathExists = true;
dialog1.Filter = "Excel Workbooks (*.xls) | *.xls";
dialog1.OverwritePrompt = true;
dialog1.Title = "Save Excel Formatted Report";
if (dialog1.ShowDialog() == DialogResult.OK)
大枣红糖
{
网易邮箱126邮箱
int num2 = 0;
int num3 = mDSData.Tables[0].Rows.Count + 1;
int num1 = mDSData.Tables[0].Columns.Count;
num2 = 0;
string text1 = dialog1.FileName;
if (File.Exists(text1))
{
File.Delete(text1);
}
StreamWriter writer1 = new StreamWriter(text1, fal);
StreamWriter writer2 = writer1;
writer2.WriteLine("<?xml version=\"1.0\"?>");
writer2.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");
无忧购物writer2.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
writer2.WriteLine(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
writer2.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
writer2.WriteLine(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
writer2.WriteLine(" xmlns:html=\"www.w3/TR/REC-html40\">");
瓶子画
writer2.WriteLine(" <DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">");
writer2.WriteLine("  <Author>Automated Report Generator Example</Author>");
writer2.WriteLine(string.Format("  <Created>{0}T{1}Z</Created>", DateTime.Now.ToString("yyyy-mm-dd"), DateTime.Now.ToString("HH:MM:SS")));
writer2.WriteLine("  <Company>Your Company Here</Company>");
writer2.WriteLine("  <Version>11.6408</Version>");
writer2.WriteLine(" </DocumentProperties>");
writer2.WriteLine(" <ExcelWorkbook xmlns=
\"urn:schemas-microsoft-com:office:excel\">");
writer2.WriteLine("  <WindowHeight>8955</WindowHeight>");
writer2.WriteLine("  <WindowWidth>11355</WindowWidth>");
writer2.WriteLine("  <WindowTopX>480</WindowTopX>");
writer2.WriteLine("  <WindowTopY>15</WindowTopY>");
writer2.WriteLine("  <ProtectStructure>Fal</ProtectStructure>");
writer2.WriteLine("  <ProtectWindows>Fal</ProtectWindows>");
writer2.WriteLine(" </ExcelWorkbook>");
writer2.WriteLine(" <Styles>");
writer2.WriteLine("  <Style ss:ID=\"Default\" ss:Name=\"Normal\">");
writer2.WriteLine("  <Alignment ss:Vertical=\"Bottom\"/>");
writer2.WriteLine("  <Borders/>");
writer2.WriteLine("  <Font/>");
writer2.WriteLine("  <Interior/>");
writer2.WriteLine("  <Protection/>");
writer2.WriteLine("  </Style>");
writer2.WriteLine("  <Style ss:ID=\"s21\">");
writer2.WriteLine("  <Alignment ss:Vertical=\"Bottom\" ss:WrapText=\"1\"/>");
writer2.WriteLine("  </Style>");
writer2.WriteLine(" </Styles>");
writer2.WriteLine(" <Worksheet ss:Name=\"MyReport\">");
writer2.WriteLine(string.Format("  <Table ss:ExpandedColumnCount=\"{0}\" ss:ExpandedRowCount=\"{1}\" x:FullColumns=\"1\"", num1.ToString(), num3.ToString()));
writer2.WriteLine("  x:FullRows=\"1\">");
foreach (DataRow row1 in mDSData.Tables[0].Rows)
{
writer2.WriteLine("<Row>");
for (num2 = 0; num2 != num1; num2++)
{
writer2.Write("<Cell ss:StyleID=\"s21\"><Data ss:Type=\"String\">");
writer2.Write(row1[num2].ToString());
writer2.WriteLine("</Data></Cell>");
}
writer2.WriteLine("</Row>");
}
初孕有什么症状
writer2.WriteLine("  </Table>");
writer2.WriteLine("  <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
writer2.WriteLine("  <Selected/>");
writer2.WriteLine("  <Panes>");
writer2.WriteLine("    <Pane>");
writer2.WriteLine("    <Number>3</Number>");
writer2.WriteLine("    <ActiveRow>1</ActiveRow>");
writer2.WriteLine("    </Pane>");
writer2.WriteLine("  </Panes>");
writer2.WriteLine("  <ProtectObjects>Fal</ProtectObjects>");
writer2.WriteLine("  <ProtectScenarios>Fal</ProtectScenarios>");
writer2.WriteLine("  </WorksheetOptions>");
writer2.WriteLine(" </Worksheet>");
writer2.WriteLine(" <Worksheet
ss:Name=\"Sheet2\">");
writer2.WriteLine("  <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
火车是高铁吗writer2.WriteLine("  <ProtectObjects>Fal</ProtectObjects>");
writer2.WriteLine("  <ProtectScenarios>Fal</ProtectScenarios>");
writer2.WriteLine("  </WorksheetOptions>");
writer2.WriteLine(" </Worksheet>");寻找的英文短语
writer2.WriteLine(" <Worksheet ss:Name=\"Sheet3\">");
writer2.WriteLine("  <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
writer2.WriteLine("  <ProtectObjects>Fal</ProtectObjects>");
writer2.WriteLine("  <ProtectScenarios>Fal</ProtectScenarios>");
writer2.WriteLine("  </WorksheetOptions>");
writer2.WriteLine(" </Worksheet>");
writer2.WriteLine("</Workbook>");
writer2 = null;
writer1.Clo();
MessageBox.Show("Report Created", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}

本文发布于:2023-07-20 19:57:23,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1107538.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:使用   代码   创建   邮箱   文档   需要
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图