static DataTable GetTable()
{
DataTable table = new DataTable(); // New data table.
table.Columns.Add("Dosage", typeof(int)); // Add five columns.
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
 
table.Rows.Add(15, "Abilify", "xxx", DateTime.Now); // Add five data rows.
table.Rows.Add(40, "Accupril", "yyy", DateTime.Now);
table.Rows.Add(40, "Accutane", "zzz", DateTime.Now);
table.Rows.Add(20, "Aciphex", "zyy", DateTime.Now);
table.Rows.Add(45, "Actos", "xxxy", DateTime.Now);
 
return table; // Return reference.
}
 
private void exportDataTableToExcel(DataTable dt, string filePath)
{
// Excel file Path
string myFile = filePath;
 
//System.Data.DataRow dr = default(System.Data.DataRow);
 
int colIndex = 0;
int rowIndex = 0;
 
// Open the file and write the headers
StreamWriter fs = new StreamWriter(myFile, false);
 
fs.WriteLine("<? xml version=\"1.0\"?>");
fs.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");
fs.WriteLine("<ss:Workbook xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">");
 
// Create the styles for the worksheet
fs.WriteLine(" <ss:Styles>");
// Style for the column headers
fs.WriteLine(" <ss:Style ss:ID=\"1\">");
fs.WriteLine(" <ss:Font ss:Bold=\"1\" ss:Color=\"#FFFFFF\"/>");
fs.WriteLine(" <ss:Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\" " + "ss:WrapText=\"1\"/>");
fs.WriteLine(" <ss:Interior ss:Color=\"#254117\" ss:Pattern=\"Solid\"/>");
fs.WriteLine(" </ss:Style>");
// Style for the column information
fs.WriteLine(" <ss:Style ss:ID=\"2\">");
fs.WriteLine(" <ss:Alignment ss:Vertical=\"Center\" ss:WrapText=\"1\"/>");
fs.WriteLine(" </ss:Style>");
// Style for the column headers
fs.WriteLine(" <ss:Style ss:ID=\"3\">");
fs.WriteLine(" <ss:Font ss:Bold=\"1\" ss:Color=\"#FFFFFF\"/>");
fs.WriteLine(" <ss:Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\" " + "ss:WrapText=\"1\"/>");
fs.WriteLine(" <ss:Interior ss:Color=\"#736AFF\" ss:Pattern=\"Solid\"/>");
fs.WriteLine(" </ss:Style>");
fs.WriteLine(" </ss:Styles>");
 
// Write the worksheet contents
fs.WriteLine("<ss:Worksheet ss:Name=\"Sheet1\">");
fs.WriteLine(" <ss:Table>");
 
fs.WriteLine(" <ss:Row>");
foreach (DataColumn dc in dt.Columns)
{
fs.WriteLine(string.Format(" <ss:Cell ss:StyleID=\"1\">" + "<ss:Data ss:Type=\"String\">{0}</ss:Data></ss:Cell>", dc.ColumnName ));
}
 
fs.WriteLine(" </ss:Row>");
 
object cellText = null;
 
// Write contents for each cell
foreach (DataRow dr in dt.Rows)
{
rowIndex = rowIndex + 1;
colIndex = 0;
fs.WriteLine(" <ss:Row>");
foreach (DataColumn dc in dt.Columns)
{
cellText = dr[dc];
// Check for null cell and change it to empty to avoid error
if (cellText == null ) cellText = "";
fs.WriteLine(string.Format(" <ss:Cell ss:StyleID=\"2\">" +
"<ss:Data ss:Type=\"String\">{0}</ss:Data></ss:Cell>", cellText));
colIndex = colIndex + 1;
}
fs.WriteLine(" </ss:Row>");
}
 
fs.WriteLine(" <ss:Row>");
fs.WriteLine(" </ss:Row>");
 
// Close up the document
fs.WriteLine(" </ss:Table>");
fs.WriteLine("</ss:Worksheet>");
fs.WriteLine("</ss:Workbook>");
fs.Close();
}
 
Example:
exportDataTableToExcel (GetTable(),"C:\\PatientDetails.xls");
will write the content of data table with Formatting Styles. 

export DataTable To Excel(C)的更多相关文章

  1. csharp: Export DataTable to Excel using OpenXml 2.5 in asp.net

    //https://www.microsoft.com/en-us/download/details.aspx?id=5124 Open XML SDK 2.0 for Microsoft Offic ...

  2. csharp: Export DataSet into Excel and import all the Excel sheets to DataSet

    /// <summary> /// Export DataSet into Excel /// </summary> /// <param name="send ...

  3. asp.net DataTable导出Excel 自定义列名

    1.添加引用NPOI.dll 2.cs文件头部添加 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System.IO; 3.代码如 ...

  4. DataTable to Excel(使用NPOI、EPPlus将数据表中的数据读取到excel格式内存中)

    /// <summary> /// DataTable to Excel(将数据表中的数据读取到excel格式内存中) /// </summary> /// <param ...

  5. NetSuite SuiteScript 2.0 export data to Excel file(xls)

    In NetSuite SuiteScript, We usually do/implement export data to CSV, that's straight forward: Collec ...

  6. Datatable导出Excel

    ; IRow headerRow = sheet.CreateRow(); ; ; ; iRowIndex++; } ; i < icolIndex; i++) { sheet.AutoSize ...

  7. 使用Aspose将DataTable转Excel

    0.准备工作   1.下载并引入Aspose.Cells 下载Aspose Cells并引入using Aspose.Cells 下面示例中用的是.net 3.0版本的Aspose Cells,编译环 ...

  8. ashx导出dataTable为Excel

    一,datatable导出Excel,用户可以选择路径,方法如下: /// <summary> /// DataTable导出到Excel /// </summary> /// ...

  9. c# Datatable导出Excel

    using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; ...

随机推荐

  1. 服装盘点机PDA在服装行业颜色尺码仓库条码高效管理应用

    服装行业的商品管理的特点是需要管理颜色和尺码 具体逻辑就是: 什么商品,什么颜色,什么尺码,入库多少个? 什么商品,什么颜色,什么尺码,出库多少个? 什么商品,什么颜色,什么尺码,还有库存多少个? 如 ...

  2. 《Pro SQL Server Internals, 2nd edition》的CHAPTER 2 Tables and Indexes中的Clustered Indexes一节(翻译)

    <Pro SQL Server Internals> 作者: Dmitri Korotkevitch 出版社: Apress出版年: 2016-12-29页数: 804定价: USD 59 ...

  3. Hyperledger Fabric网络节点架构

    Fabric区块链网络的组成  区块链网络结构图 区块链网络组成 组成区块链网络相关的节点 节点是区块链的通信主体,和区块链网络相关的节点有多种类型:客户端(应用).Peer节点.排序服务(Orde ...

  4. vector读入指定行数但不指定列数的数字

    #include <iostream> #include <vector> #include <cstdio> #include <cstring> # ...

  5. 【SE】Week1 : 个人博客作业

    快速看完整部教材,列出你不懂的 5 - 10 个问题,发布在你的个人博客上. 1)针对书中提到的NABCD模型中的N,如何发掘市场不明确的潜在用户需求? 2)PM是否负责团队职责的分配以及工程模块的设 ...

  6. Linux内核分析 一二章读书笔记

    第一章 Linux内核简介 1.Unix (1)Unix系统很简洁 (2)在Unix中,所以东西都被当作文件对待,通过一套相同的系统调用接口来进行:open(),read(),write(),lsee ...

  7. 第二个Sprint冲刺总结

    第二个Sprint冲刺总结 ( 1)团队Github: https://github.com/ouqifeng/EasyGoOperation.git ( 2 ) 团队贡献分: 廖焯燊:22 何武鹏: ...

  8. 『编程题全队』Beta 阶段冲刺博客集合

    『编程题全队』Beta 阶段冲刺博客集合 »敏捷冲刺 日期:2018.5.23 博客连接:『编程题全队』Scrum 冲刺博客 »Day1 日期:2018.5.23 博客连接:『编程题全队』Beta 阶 ...

  9. Error -26631: HTTP Status-Code=400 (Bad Request) for

    最近在做性能测试,在开发web脚本的过程中遇到错误:Action.c(15): Error -26631: HTTP Status-Code=400 (Bad Request) for http:// ...

  10. 【题解】第n小的数

    题目描述 给你一些整数,你要找出第n小的数,也就是说把数据按升序排序后,输出下标是n的那个数,注意,我们规定下标是从0开始的.但是我们给出的数据的格式比较奇怪,我们是按区间给出,例如:给出两个区间:[ ...