C#利用XML创建Excel文档的实现方法
作者:shichen2014 发布时间:2022-12-21 07:15:53
标签:C#,XML,创建,Excel
一般来说C#在不安装Excel软件的情况下,可以通过XML来创建Excel文档。因此,运行本文所述代码您无需安装Excel程序。本文原例子是使用VB.Net写的,以下的用C#改写的代码,分享给大家,供大家参考。
具体代码如下:
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)
{
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, false);
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=\"http://www.w3.org/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>False</ProtectStructure>");
writer2.WriteLine(" <ProtectWindows>False</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>False</ProtectObjects>");
writer2.WriteLine(" <ProtectScenarios>False</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>False</ProtectObjects>");
writer2.WriteLine(" <ProtectScenarios>False</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>False</ProtectObjects>");
writer2.WriteLine(" <ProtectScenarios>False</ProtectScenarios>");
writer2.WriteLine(" </WorksheetOptions>");
writer2.WriteLine(" </Worksheet>");
writer2.WriteLine("</Workbook>");
writer2 = null;
writer1.Close();
MessageBox.Show("Report Created", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
这只是主要的代码,使用前需要此入using相应的命名空间,如果不知道需要哪个命名空间,可在编译时根据提示逐个添加。


猜你喜欢
- 1.任何一门编程语言均有相关数据类型。C#也不例外,其基本数据类型有int,short,long,float,double,string等。
- 在用C#开发Web应用时有个痛点,就是本机用VS开启Web应用调试时外部机器无法访问此Web应用。这里将会介绍如何通过设置允许局域网和外网机
- 一、使用方式可以采用Transactional,配置propagation即可。打开org.springframework.transact
- 在定义一个Rest接口时通常会利用GET、POST、PUT、DELETE来实现数据的增删改查;这几种方式有的需要传递参数,后台开发人员必须对
- 目录了解程序集如何在C#.NET中加载程序集,模块和引用.NET中的程序集绑定绑定重定向当问题开始发生时故障排除边注References了解
- 在常见场景下:返回数据建议使用map,不建议使用实体对象 /** * 1. 名字包含雨并且年龄小于40  
- 首先是获取特定进程对象,可以使用Process.GetProcesses()方法来获取系统中运行的所有进程,或者使用Process.GetC
- 图的实际应用在现实生活中,有许多应用场景会包含很多点以及点点之间的连接,而这些应用场景我们都可以用即将要学习的图这种数据结构去解决。地图:我
- 一、实验题目二、分析实验要求为:实现一个界面,界面中包含一个文本显示区和两个按钮(存档和读档)读档按钮作用是打开文件并读取内容,将内容显示在
- 本文实例为大家分享了Java使用poi操作excel的具体代码,供大家参考,具体内容如下依赖poi的jar包,pom.xml配置如下:<
- 效果展示在实际项目当中我们经常看到如下各种剪裁形状的效果,Flutter 为我们提供了非常方便的 Widget 很轻松就可以实现,下面我们来
- 简介本文介绍InheritableThreadLocal的用法。ThreadLocal可以将数据绑定当前线程,如果希望当前线程的Thread
- 有时候我们在阅读PDF文档时会遇到这样一种情况:PDF文档页数比较多,但是又没有书签,所以我们不能根据书签快速了解文档所讲解的内容,也不能点
- 1、未配置之前2、开始配置 2.1 新建一个unauth.html<!DOCTYPE html><html la
- SpeSqliteManager4Android改动日志2023.2.14 完成SQLiteOpenHelper 2023.2.23 完成r
- 本文实例讲述了Java简单实现调用命令行并获取执行结果。分享给大家供大家参考,具体如下:import java.io.BufferedRea
- 只能输入数字:"^[0-9]*$"。只能输入n位的数字:"^\d{n}$"。只能输入至少n位的数字:
- 本文实例讲述了C#使用foreach语句遍历队列(Queue)的方法。分享给大家供大家参考。具体如下:using System;using
- 本文将基于Spring Boot介绍两种生成二维码的实现方式,一种是基于Google开发工具包,另一种是基于Hutool来实现;
- Android自带的SeekBar是水平的,要垂直的,必须自己写一个类,继承SeekBar。一个简单的垂直SeekBar的例子:(但是它其实