xml方式将dataset导出excel
Posted 张文斌
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml方式将dataset导出excel相关的知识,希望对你有一定的参考价值。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
namespace Cinway.Common
{
public class xmlParam {
public string totalcount { get; set; }
public string startdt { get; set; }
public string enddt { get; set; }
public string xmlNum { get; set; }
public string deptcode { get; set; }
public string deptname { get; set; }
public string strstart { get; set; }
public string jszhu { get; set; }
public string jszhang { get; set; }
public string commonstr { get; set; }
public string filename { get; set; }//新增zp
}
public class ExcelToolNew
{
public FileStream ToExcel(DataSet ds,string headstr,string footstr,xmlParam pa) {
DataTable dt = ds.Tables[0];
int rowcount = dt.Rows.Count;
int rcount = dt.Rows.Count / 60;
DataSet dataset = SplitDataTable(dt, rcount);
dt.Clear();
string[] strfilename = new string[dataset.Tables.Count];
ManualResetEvent[] doneEvents = new ManualResetEvent[dataset.Tables.Count];
Fibonacci[] fibArray = new Fibonacci[dataset.Tables.Count];
int i = 0;
foreach (DataTable datatable in dataset.Tables)
{
string sfilename = "D:\\xmlfile\\" + Guid.NewGuid() + ".XML";
strfilename[i] = sfilename;
Fibonacci fi = new Fibonacci();
fi.WriteXML(datatable, sfilename, pa.xmlNum,pa.strstart);
i++;
}
string filename = "D:\\xmlfile\\" + Guid.NewGuid() + ".xml";
StreamReader srh = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(headstr), Encoding.Default);//读出表头部内容
FileStream fs1 = new FileStream(filename, FileMode.OpenOrCreate);
StreamWriter sw1 = new StreamWriter(fs1, Encoding.UTF8);
sw1.WriteLine(srh.ReadToEnd().Replace("totalcount", pa.totalcount).Replace("StartDT", pa.startdt).Replace("EndDT",pa.enddt).Replace("deptcode", pa.deptcode).Replace("deptname", pa.deptname).Replace("jszhang", pa.jszhang).Replace("jszhu", pa.jszhu).Replace("commonstr", pa.commonstr));//把头部内容写到新文件中
for (int k = 0; k < strfilename.Length; k++)
{
StreamReader sr1 = new StreamReader(strfilename[k], Encoding.Default);
sw1.Write(sr1.ReadToEnd());//插入数据
}
StreamReader srf = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(footstr), Encoding.Default);//读出表尾部内容
sw1.WriteLine(srf.ReadToEnd());
sw1.Close();
fs1.Close();
FileStream fsm = new FileStream(filename, FileMode.Open);
return fsm;
}
public string ToExcelnew(DataSet ds, string headstr, string footstr, xmlParam pa)
{
DataTable dt = ds.Tables[0];
int rowcount = dt.Rows.Count;
int rcount = dt.Rows.Count / 60;
DataSet dataset = SplitDataTable(dt, rcount);
dt.Clear();
string[] strfilename = new string[dataset.Tables.Count];
ManualResetEvent[] doneEvents = new ManualResetEvent[dataset.Tables.Count];
Fibonacci[] fibArray = new Fibonacci[dataset.Tables.Count];
int i = 0;
foreach (DataTable datatable in dataset.Tables)
{
string sfilename = "D:\\xmlfile\\" + Guid.NewGuid() + ".XML";
strfilename[i] = sfilename;
Fibonacci fi = new Fibonacci();
fi.WriteXML(datatable, sfilename, pa.xmlNum, pa.strstart);
i++;
}
string filename = "D:\\xmlfile\\" + Guid.NewGuid() + ".xml";
StreamReader srh = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(headstr), Encoding.Default);//读出表头部内容
FileStream fs1 = new FileStream(filename, FileMode.OpenOrCreate);
StreamWriter sw1 = new StreamWriter(fs1, Encoding.UTF8);
sw1.WriteLine(srh.ReadToEnd().Replace("totalcount", pa.totalcount).Replace("StartDT", pa.startdt).Replace("EndDT", pa.enddt).Replace("deptcode", pa.deptcode).Replace("deptname", pa.deptname).Replace("jszhang", pa.jszhang).Replace("jszhu", pa.jszhu).Replace("commonstr", pa.commonstr));//把头部内容写到新文件中
for (int k = 0; k < strfilename.Length; k++)
{
StreamReader sr1 = new StreamReader(strfilename[k], Encoding.Default);
sw1.Write(sr1.ReadToEnd());//插入数据
}
StreamReader srf = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(footstr), Encoding.Default);//读出表尾部内容
sw1.WriteLine(srf.ReadToEnd());
sw1.Close();
fs1.Close();
string strfileName = Common.SecurityEncrypt.ESCEncrypt(pa.filename, "20170421");
string strfilePath = Common.SecurityEncrypt.ESCEncrypt(filename, "20170421");
//FileStream fsm = new FileStream(filename, FileMode.Open);
return "filename=" + strfileName + "&filepath=" + strfilePath;
}
// DateTime enddt = DateTime.Now;
// FileStream fss = new FileStream("C:\\JISHI.TXT", FileMode.OpenOrCreate);
// StreamWriter sws = new StreamWriter(fss, Encoding.UTF8);
//sws.WriteLine(startdt.ToString() + "******" + enddt.ToString());
// sws.Close();
// fss.Close();
/// <summary>
/// 分解数据表
/// </summary>
/// <param name="originalTab">需要分解的表</param>
/// <param name="rowsNum">每个表包含的数据量</param>
/// <returns></returns>
public DataSet SplitDataTable(DataTable originalTab, int rowsNum)
{
//获取所需创建的表数量
if (rowsNum==0) {
rowsNum = 1;
}
int tableNum = originalTab.Rows.Count / rowsNum;
//获取数据余数
int remainder = originalTab.Rows.Count % rowsNum;
DataSet ds = new DataSet();
//如果只需要创建1个表,直接将原始表存入DataSet
if (tableNum == 0)
{
if (originalTab.Rows.Count==0) {
ds.Tables.Add(new DataTable());
}
else
{
ds.Tables.Add(originalTab);
}
}
else
{
DataTable[] tableSlice = new DataTable[tableNum];
//Save orginal columns into new table.
for (int c = 0; c < tableNum; c++)
{
tableSlice[c] = new DataTable();
foreach (DataColumn dc in originalTab.Columns)
{
tableSlice[c].Columns.Add(dc.ColumnName, dc.DataType);
}
}
//Import Rows
for (int i = 0; i < tableNum; i++)
{
// if the current table is not the last one
if (i != tableNum - 1)
{
for (int j = i * rowsNum; j < ((i + 1) * rowsNum); j++)
{
tableSlice[i].ImportRow(originalTab.Rows[j]);
}
}
else
{
for (int k = i * rowsNum; k < ((i + 1) * rowsNum + remainder); k++)
{
tableSlice[i].ImportRow(originalTab.Rows[k]);
}
}
}
//add all tables into a dataset
foreach (DataTable dt in tableSlice)
{
ds.Tables.Add(dt);
}
}
return ds;
}
public class Fibonacci
{
// ManualResetEvent _doneEvent;
public void WriteXML(DataTable dt, string filename,string xmlNum,string strstart)
{
FileStream fs1 = new FileStream(filename, FileMode.OpenOrCreate);
StreamWriter sw1 = new StreamWriter(fs1, Encoding.UTF8);
sw1.WriteLine("");
sw1.Close();
fs1.Close();
if (string.IsNullOrEmpty(strstart)) {
strstart = "<Cell ss:StyleID=\"" + xmlNum + "\"><Data ss:Type=\"String\">";
}
string strend = "</Data></Cell>";
int jishu = 0;
string xmlstr2 = "";
foreach (DataRow row in dt.Rows)
{
if (jishu == 10)
{
FileStream fs3 = new FileStream(filename, FileMode.Append);
StreamWriter sw3 = new StreamWriter(fs3, Encoding.UTF8);
sw3.WriteLine(xmlstr2);
sw3.Close();
fs3.Close();
xmlstr2 = "";
jishu = 0;
}
xmlstr2 += "<Row ss:AutoFitHeight=\"0\" ss:Height=\"18.9375\">";
for (int i = 0; i < row.ItemArray.Length; i++)
{
string str = row[i] == null ? "" : row[i].ToString();
if (str.Contains("<") || str.Contains(">"))
{
str = str.Replace(‘<‘, ‘<‘);
str = str.Replace(‘>‘, ‘>‘);
}
xmlstr2 += (strstart + str + strend);
}
xmlstr2 += "</Row>";
jishu++;
}
dt.Clear();
FileStream fs2 = new FileStream(filename, FileMode.Append);
StreamWriter sw2 = new StreamWriter(fs2, Encoding.UTF8);
sw2.WriteLine(xmlstr2);
sw2.Close();
fs2.Close();
}
}
}
}
以上是关于xml方式将dataset导出excel的主要内容,如果未能解决你的问题,请参考以下文章
求将dataset数据集导出到excel文件的方法(vs2005 c#)
excel导出 按dataset里的datatable导出一个excel多个sheet 和 datatable导出