使用XML创建Excel文档

摘要:
SampleGuid=ddbaecb9-a260-4656-9f22-300b6a1ce66c此示例使用XML创建Excel文档,但不需要在运行时安装Excel程序。

原例子使用VB.Net写的,以下的用C#改写的代码

原文代码:

http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=ddbaecb9-a260-4656-9f22-300b6a1ce66c

本例使用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)
            {
                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);
            }


免责声明:文章转载自《使用XML创建Excel文档》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇PHP解决网站大流量与高并发最小有向生成树下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(已弃用)

前言 1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请求参数 运行完后,重新生成一个excel报告,结果写入excel 用unittest+ddt数据驱动模式执行 HTMLTestRunner生成可视化的h...

PHPExcel内存泄漏问题

使用PHPExcel来生成 excel 文档是比较消耗内存的,有时候可能会需要通过一个循环来把大数据切分成若干个小的 excel 文档保存来避免内存耗尽。 然而 PHPExcel 存在 circular references 的情况(貌似在最新的 1.6.5 版本中仍然没有去解决这个问题),如果在一次 http 请求过程中反复多次构建 PHPExcel 及...

使用QtXlsx来读写excel文件

概述:QtXlsx是功能非常强大和使用非常方便的操作excel类库。包括对excel数据读写、excel数据格式设置及在excel里面根据数据生成各种图表。 下面重点介绍如何安装和使用QtXlsx。 一、获取QtXlsx。 1、通过下面地址获取:https://github.com/dbzhang800/QtXlsxWriter 2、得到的是包括源码、各种...

POI导出excel模板三种方式

POI简介 POI是Apache软件基金会用java编写的免费开源的跨平台的Java API,提供API给java程序对Microsoft Office格式档案读和写的功能,一般用来操作Excel文件。用javaPOI导出Excel时,需要考虑Excel版本和数据量的问题。 JavaPOI导出Excel有三种形式: (1) 第一种HSSFWorkbook...

OpenXml Excel数据导入导出(含图片的导入导出)

声明:里面的很多东西是基于前人的基础上实现的,具体是哪些人 俺忘了,我做了一些整合和加工 这个项目居于openxml做Excel的导入导出,可以用OpenXml读取Excel中的图片 和OpenXml插入Excel 图片,相信这两个还是挺有用的 OpenXmlHelper 类为对外抛出的类,包含封装的导入导出的数据操作方法和一些对象的属性 一、导出Exce...

excel下划线转驼峰公式

最近为了省事,决定从excel将表结构生成jquery.datatable的json对象结构,其中要把下划线转驼峰,如下: =LEFT(C251,1)&MID(SUBSTITUTE(PROPER(C251),"_",""),2,100)...