C#使用Datatable導(dǎo)出Excel
本文實(shí)例為大家分享了C#使用Datatable導(dǎo)出Excel的具體代碼,供大家參考,具體內(nèi)容如下
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
namespace EasyFrame.Common
{
/// <summary>
///title調(diào)用信息
/// </summary>
public class ChartTitle
{
/// <summary>
/// 調(diào)用名
/// </summary>
public string CallName { get; set; }
/// <summary>
/// 標(biāo)頭信息
/// </summary>
public List<ChartName> SetNameList { get; set; }
}
public class ChartName
{
/// <summary>
/// 一級(jí)標(biāo)頭
/// </summary>
public string Name { get; set; }
/// <summary>
/// 二級(jí)標(biāo)頭
/// </summary>
public List<string> ChartChildNameList { get; set; }
}
public class ExcelHelper
{
//
/// <summary>
/// Datatable導(dǎo)出Excel
/// </summary>
/// <param name="dt">數(shù)據(jù)</param>
/// <param name="list">表頭</param>
/// <param name="counts">總條數(shù)</param>
/// <param name="filePath">保存地址</param>
/// <returns></returns>
public static MemoryStream WriteExcel(DataTable dt, List<ChartTitle> objlist, string filePath)
{
var list = objlist.FirstOrDefault(a => a.CallName == "ccc").SetNameList;
if (!string.IsNullOrEmpty(filePath) && null != dt && dt.Rows.Count > 0)
{
XSSFWorkbook book = new XSSFWorkbook();
NPOI.SS.UserModel.ISheet sheet = book.CreateSheet(dt.TableName);
ICell cell = null;
ICellStyle style = book.CreateCellStyle();
IFont font = book.CreateFont();//創(chuàng)建字體樣式
IFont fonts = book.CreateFont();//創(chuàng)建字體樣式
IFont fontss = book.CreateFont();//創(chuàng)建字體樣式
font.Color = NPOI.HSSF.Util.HSSFColor.Red.Index;//設(shè)置字體顏色
ICellStyle styles = book.CreateCellStyle(); //紅色
fonts.Color = NPOI.HSSF.Util.HSSFColor.Green.Index;
ICellStyle styless = book.CreateCellStyle(); //綠色
fontss.Color = NPOI.HSSF.Util.HSSFColor.Blue.Index;
ICellStyle stylesss = book.CreateCellStyle(); //藍(lán)色
styless.SetFont(fonts);
styles.SetFont(font);
stylesss.SetFont(fontss);
style.Alignment = HorizontalAlignment.Center;
NPOI.SS.UserModel.IRow row1 = sheet.CreateRow(0);
NPOI.SS.UserModel.IRow row2 = sheet.CreateRow(1);
var row = row1;
int num = 0;//記錄列
var d = true;//是否第一次加載到二級(jí)表單
#region 創(chuàng)建表頭
//兩行單列
for (int i = 0; i < list.Count; i++)
{
if (!d)
{
row = row1;
d = true;
}
if (list[i].ChartChildNameList == null)
{
cell = row.CreateCell(num);
sheet.SetColumnWidth(num, 15 * 256);
cell.SetCellValue(list[i].Name);
cell.CellStyle = style;
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 1, num, num));
num++;
continue;
}
//兩行多列
var count = list[i].ChartChildNameList.Count; //列數(shù)
cell = row.CreateCell(num);
cell.SetCellValue(list[i].Name);
cell.CellStyle = style;
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, num, count + num - 1));
if (d)
{
d = false;
row = row2;
}
var t = 0;
var rang = sheet.GetRow(1);
for (int j = 0; j < count; j++)
{
sheet.SetColumnWidth(num + t, 5 * 256);
cell = row.CreateCell(num + t);
cell.SetCellValue(list[i].ChartChildNameList[j]);
cell.CellStyle = style;
t++;
}
num = num + count;
}
#endregion 創(chuàng)建表頭
#region 寫(xiě)入數(shù)據(jù)
int customs = 0;//記錄列數(shù)
int clouns = 0;//記錄行數(shù)
filePath = string.Format(filePath, "XXX");
#region 導(dǎo)出操作
var data1 = JsonConvert.DeserializeObject<List<類(lèi)名>>(dt.Rows[0][0].ToString());
foreach (var item in data1)
{
NPOI.SS.UserModel.IRow rows = sheet.CreateRow(clouns + 2);
customs = 0;
rows.CreateCell(customs).SetCellValue(Convert.ToString(item.No)); customs++;
rows.CreateCell(customs).SetCellValue(Convert.ToString(item.LotteryOpenNo)); customs++;
for (int i = 0; i < item.Wan.Length; i++)
{
if (item.LotteryOpenNo.Split(',')[0] == item.Wan[i].ToString())
{
cell = rows.CreateCell(customs);
cell.SetCellValue(item.Wan[i]);
cell.CellStyle = styles;
customs++;
}
else
{
rows.CreateCell(customs).SetCellValue(item.Wan[i]); customs++;
}
}
for (int j = 0; j < item.Qian.Length; j++)
{
if (item.LotteryOpenNo.Split(',')[1] == item.Qian[j].ToString())
{
cell = rows.CreateCell(customs);
cell.SetCellValue(item.Qian[j]);
cell.CellStyle = styles;
customs++;
}
else
{
rows.CreateCell(customs).SetCellValue(item.Qian[j]); customs++;
}
}
for (int k = 0; k < item.Bai.Length; k++)
{
if (item.LotteryOpenNo.Split(',')[2] == item.Bai[k].ToString())
{
cell = rows.CreateCell(customs);
cell.SetCellValue(item.Bai[k]);
cell.CellStyle = styles;
customs++;
}
else
{
rows.CreateCell(customs).SetCellValue(item.Bai[k]); customs++;
}
}
for (int l = 0; l < item.Shi.Length; l++)
{
if (item.LotteryOpenNo.Split(',')[3] == (item.Shi[l].ToString()))
{
cell = rows.CreateCell(customs);
cell.SetCellValue(item.Shi[l]);
cell.CellStyle = styles;
customs++;
}
else
{
rows.CreateCell(customs).SetCellValue(item.Shi[l]); customs++;
}
}
for (int m = 0; m < item.Ge.Length; m++)
{
if (item.LotteryOpenNo.Split(',')[4] == (item.Ge[m].ToString()))
{
cell = rows.CreateCell(customs);
cell.SetCellValue(item.Ge[m]);
cell.CellStyle = styles;
customs++;
}
else
{
rows.CreateCell(customs).SetCellValue(item.Ge[m]); customs++;
}
}
for (int n = 0; n < item.Fen.Length; n++)
{
if (item.LotteryOpenNo.Contains(item.Fen[n].ToString()))
{
cell = rows.CreateCell(customs);
cell.SetCellValue(item.Fen[n]);
cell.CellStyle = styles;
customs++;
}
else
{
rows.CreateCell(customs).SetCellValue(item.Fen[n]); customs++;
}
}
clouns++;
}
#endregion
#endregion 寫(xiě)入數(shù)據(jù)
// 寫(xiě)入到客戶端
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
book.Write(ms);
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
byte[] data = ms.ToArray();
fs.Write(data, 0, data.Length);
fs.Flush();
}
book = null;
return ms;
}
}
return null;
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C#圖像處理的多種方法
欄 目:C#教程
下一篇:c#基礎(chǔ)系列之System.String的深入理解
本文標(biāo)題:C#使用Datatable導(dǎo)出Excel
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5079.html
您可能感興趣的文章
- 01-10C#使用Dispose模式實(shí)現(xiàn)手動(dòng)對(duì)資源的釋放
- 01-10C#3.0使用EventLog類(lèi)寫(xiě)Windows事件日志的方法
- 01-10C#使用windows服務(wù)開(kāi)啟應(yīng)用程序的方法
- 01-10c# ArrayList的使用方法小總結(jié)
- 01-10C#使用ADO.Net部件來(lái)訪問(wèn)Access數(shù)據(jù)庫(kù)的方法
- 01-10C#使用Mutex簡(jiǎn)單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
- 01-10C#導(dǎo)出網(wǎng)站功能實(shí)例代碼講解
- 01-10C#中yield用法使用說(shuō)明
- 01-10C#編程和Visual Studio使用技巧(下)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置


