C#中写入Excel



using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Web;
using  System.Data;
namespace  Common.BaseClass
{
public   class  FileExport
{
#region  导出文件为 Excel文件 文本格式
///   <summary>
///  导出文件为 Excel文件
///   </summary>
///   <param name="dt"> 数据集 </param>
///   <param name="FileName"> 导出文件名称 </param>
///   <param name="headers"> 列名 </param>
///   <param name="remark"> 备注 </param>
public   void  CreateExcel(DataTable dt,  string  FileName,  string [] headers,  string [] remark)
{

HttpResponse resp;
resp 
=  HttpContext.Current.Response;
resp.ContentEncoding 
=  System.Text.Encoding.GetEncoding( " GB2312 " );
resp.AppendHeader(
" Content-Disposition " " attachment;filename= "   +  FileName);
// 标题 内容
string  colHeaders  =   "" , ls_item  =   "" ;
int  i  =   0 ;

DataRow[] myRow 
=  dt.Select( "" );
for  (i  =   0 ; i  <  remark.Length; i ++ )
{
colHeaders 
+=  remark[i]  +   " \n " ;
}
// 取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
for  (i  =   0 ; i  <  headers.Length; i ++ )
{
colHeaders 
+=  headers[i]  +   " \t " ;
}
colHeaders 
+=   " \n " ;
// 向HTTP输出流中写入取得的数据信息
resp.Write(colHeaders);
// 逐行处理数据
foreach  (DataRow row  in  myRow)
{
// 在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n
for  (i  =   0 ; i  <  dt.Columns.Count; i ++ )
{
ls_item 
+=  row[i].ToString()  +   " \t " ;
// 当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
}
ls_item 
+=   " \n " ;
resp.Write(ls_item);
ls_item 
=   "" ;
}

// 写缓冲区中的数据到HTTP头文件中
resp.End();
}
#endregion
///   <summary>
///  将Dataset中的表导出到Excel中
///   </summary>
///   <param name="source"> 源数据集 </param>
///   <param name="fileName"> 保存的文件名 </param>
///   <param name="title"> 标题 </param>
///   <param name="headers"> 列名 </param>
///   <param name="explain"> 文件生成说明 </param>
///   <param name="ps"> 注释 </param>
public   static   void  ExportToExcel(DataTable source,  string  fileName,  string [] headers,  string  title,  string  explain, string  []ps)
{
HttpResponse resp;
resp 
=  HttpContext.Current.Response;
resp.ContentEncoding 
=  System.Text.Encoding.GetEncoding( " utf-8 " );
resp.AppendHeader(
" Content-Disposition " " attachment;filename= "   +  fileName);


string  startExcelXML  =   @" <?xml version=""1.0"" encoding=""utf-8""?>
<Workbook xmlns=""urn:schemas-microsoft-com:office:spreadsheet""
xmlns:o=""urn:schemas-microsoft-com:office:office""
xmlns:x=""urn:schemas- microsoft-com:office:excel""
xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet"">
<Styles>
<Style ss:ID=""Default"" ss:Name=""Normal"">
<Alignment ss:Horizontal=""Center"" ss:Vertical=""Center""/>
<Font/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID=""BoldColumn"">
<Borders>
<Border ss:Position=""Bottom"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
<Border ss:Position=""Left"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
<Border ss:Position=""Right"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
<Border ss:Position=""Top"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
</Borders>
<Font ss:FontName=""宋体"" ss:Bold=""1"" x:CharSet=""134"" ss:Size=""12"" />
<Interior ss:Color=""#D2EAF6"" ss:Pattern=""Solid""/>
</Style>
<Style ss:ID=""StringLiteral"">
<NumberFormat ss:Format=""@""/>
<Borders>
<Border ss:Position=""Bottom"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
<Border ss:Position=""Left"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
<Border ss:Position=""Right"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
<Border ss:Position=""Top"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
</Borders>
<Font ss:FontName=""宋体"" x:CharSet=""134"" ss:Size=""12"" />
</Style>
<Style ss:ID=""Decimal"">
<NumberFormat ss:Format=""0.00""/>
<Borders>
<Border ss:Position=""Bottom"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
<Border ss:Position=""Left"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
<Border ss:Position=""Right"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
<Border ss:Position=""Top"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
</Borders>
<Font ss:FontName=""宋体"" x:CharSet=""134"" ss:Size=""12"" />
</Style>
<Style ss:ID=""Integer"">
<NumberFormat ss:Format=""0""/>
<Borders>
<Border ss:Position=""Bottom"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
<Border ss:Position=""Left"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
<Border ss:Position=""Right"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
<Border ss:Position=""Top"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
</Borders>
<Font ss:FontName=""宋体"" x:CharSet=""134"" ss:Size=""12"" />
</Style>
<Style ss:ID=""DateLiteral"">
<NumberFormat ss:Format=""yyyy-MM-dd HH:mm;@""/>
<Borders>
<Border ss:Position=""Bottom"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
<Border ss:Position=""Left"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
<Border ss:Position=""Right"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
<Border ss:Position=""Top"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
</Borders>
<Font ss:FontName=""宋体"" x:CharSet=""134"" ss:Size=""12"" />
</Style>
<Style ss:ID=""ps""><!--注释-->
<Alignment ss:Horizontal=""Left"" ss:Vertical=""Center""/>
<Font ss:FontName=""宋体"" x:CharSet=""134""/>
<Interior ss:Color=""#FFFFCC"" ss:Pattern=""Solid""/>
</Style>
<Style ss:ID=""explain""><!--说明:数据跟据什么而来-->
<Alignment ss:Horizontal=""Left"" ss:Vertical=""Center""/>
<Borders>
<Border ss:Position=""Bottom"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
<Border ss:Position=""Top"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
</Borders>
<Font ss:FontName=""宋体"" x:CharSet=""134"" ss:Size=""12""/>
</Style>
<Style ss:ID=""Title""><!--大标题-->
<Alignment ss:Horizontal=""Center"" ss:Vertical=""Center""/>
<Borders>
<Border ss:Position=""Bottom"" ss:LineStyle=""Continuous"" ss:Weight=""1""/>
</Borders>
<Font ss:FontName=""宋体"" x:CharSet=""134"" ss:Size=""16"" ss:Bold=""1""/>
</Style>
</Styles>
" ;
string  endExcelXML  =   " \r\n</Workbook> " ;

int  rowCount  =   0 ;
int  sheetCount  =   0 ;
resp.Write(startExcelXML);
sheetCount
++ ;
resp.Write(
" \r\n<Worksheet ss:Name=\ ""  + title +  " \ " > " );
resp.Write(
" \r\n<Table> " );

// 写标题
resp.Write( " \r\n<Row>\r\n<Cell ss:MergeAcross=\ ""  + (source.Columns.Count - 1) +  " \ "  ss:StyleID=\ " Title\ " ><Data ss:Type=\ " String\ " > "   +  title  +   " </Data></Cell>\t\t\t\r\n</Row> " );
// 写数据来源
resp.Write( " \r\n<Row>\r\n<Cell ss:MergeAcross=\ ""  + (source.Columns.Count - 1) +  " \ "  ss:StyleID=\ " explain\ " ><Data ss:Type=\ " String\ " > "   +  explain  +   " </Data></Cell>\r\n</Row> " );

resp.Write(
" \r\n<Row> " );
// 得到数据集有多少列
// for (int x = 0; x < source.Columns.Count; x++)
// {
//  resp.Write("<Cell ss:StyleID=\"BoldColumn\"><Data ss:Type=\"String\">");
//  resp.Write(source.Columns[x].ColumnName);
//  resp.Write("</Data></Cell>");
// } // New 自己定义列名
for  ( int  x  =   0 ; x  <  headers.Length; x ++ )
{
resp.Write(
" \r\n<Cell ss:StyleID=\ " BoldColumn\ " ><Data ss:Type=\ " String\ " > " );
resp.Write(headers[x]);
resp.Write(
" </Data></Cell> " );
}
resp.Write(
" </Row> " );

// 便历出数据集中的每一行数据
foreach  (DataRow x  in  source.Rows)
{
rowCount
++ ;
// 如果行数超过 陆万肆仟行,则新起一页
if  (rowCount  ==   64000 )
{
rowCount 
=   0 ;
sheetCount
++ ;
resp.Write(
" \r\n</Table> " );
resp.Write(
"  \r\n</Worksheet> " );
resp.Write(
" \r\n<Worksheet ss:Name=\ ""  + source.TableName + sheetCount +  " \ " > " );
resp.Write(
" \r\n<Table> " );
}

resp.Write(
" \r\n<Row> " );  // ID=" + rowCount + "

for  ( int  y  =   0 ; y  <  source.Columns.Count; y ++ )
{
System.Type rowType;
// 获得将要写入数据的数据类型
rowType  =  x[y].GetType();
switch  (rowType.ToString())
{
case   " System.String " :
string  XMLstring  =  x[y].ToString();
XMLstring 
=  XMLstring.Trim();
XMLstring 
=  XMLstring.Replace( " & " " & " );
XMLstring 
=  XMLstring.Replace( " > " " > " );
XMLstring 
=  XMLstring.Replace( " < " " < " );
resp.Write(
" \r\n<Cell ss:StyleID=\ " StringLiteral\ " > "   +
" <Data ss:Type=\ " String\ " > " );
resp.Write(XMLstring);
resp.Write(
" </Data></Cell> " );
break ;
case   " System.DateTime " :
// Excel has a specific Date Format of YYYY-MM-DD followed by
// the letter 'T' then hh:mm:sss.lll Example 2005-01-31T24:01:21.000
// The Following Code puts the date stored in XMLDate
// to the format above
DateTime XMLDate  =  (DateTime)x[y];
string  XMLDatetoString  =   "" // Excel Converted Date
XMLDatetoString  =  XMLDate.Year.ToString()  +
" - "   +
(XMLDate.Month 
<   10   ?   " 0 "   +
XMLDate.Month.ToString() : XMLDate.Month.ToString()) 
+
" - "   +
(XMLDate.Day 
<   10   ?   " 0 "   +
XMLDate.Day.ToString() : XMLDate.Day.ToString()) 
+
" T "   +
(XMLDate.Hour 
<   10   ?   " 0 "   +
XMLDate.Hour.ToString() : XMLDate.Hour.ToString()) 
+
" : "   +
(XMLDate.Minute 
<   10   ?   " 0 "   +
XMLDate.Minute.ToString() : XMLDate.Minute.ToString()) 
+
" : "   +
(XMLDate.Second 
<   10   ?   " 0 "   +
XMLDate.Second.ToString() : XMLDate.Second.ToString()) 
+
" .000 " ;
resp.Write(
" \r\n<Cell ss:StyleID=\ " DateLiteral\ " > "   +
" <Data ss:Type=\ " DateTime\ " > " );
resp.Write(XMLDatetoString);
resp.Write(
" </Data></Cell> " );
break ;
case   " System.Boolean " :
resp.Write(
" \r\n<Cell ss:StyleID=\ " StringLiteral\ " > "   +
" <Data ss:Type=\ " String\ " > " );
resp.Write(x[y].ToString());
resp.Write(
" </Data></Cell> " );
break ;
case   " System.Int16 " :
case   " System.Int32 " :
case   " System.Int64 " :
case   " System.Byte " :
resp.Write(
" \r\n<Cell ss:StyleID=\ " Integer\ " > "   +
" <Data ss:Type=\ " Number\ " > " );
resp.Write(x[y].ToString());
resp.Write(
" </Data></Cell> " );
break ;
case   " System.Decimal " :
case   " System.Double " :
resp.Write(
" \r\n<Cell ss:StyleID=\ " Decimal\ " > "   +
" <Data ss:Type=\ " Number\ " > " );
resp.Write(x[y].ToString());
resp.Write(
" </Data></Cell> " );
break ;
case   " System.DBNull " :
resp.Write(
" \r\n<Cell ss:StyleID=\ " StringLiteral\ " > "   +
" <Data ss:Type=\ " String\ " > " );
resp.Write(
"" );
resp.Write(
" </Data></Cell> " );
break ;
default :
throw  ( new  Exception(rowType.ToString()  +   "  not handled. " ));
}
}
resp.Write(
" \r\n</Row> " );
}
// 注释
foreach  ( string  str  in  ps)
{
resp.Write(
" \r\n<Row>\r\n<Cell ss:MergeAcross=\ ""  + (source.Columns.Count - 1) +  " \ "  ss:StyleID=\ " ps\ " ><Data ss:Type=\ " String\ " > "   +  str  +   " </Data></Cell>\r\n</Row> " );
}
resp.Write(
" \r\n</Table> " );
resp.Write(
" \r\n</Worksheet> " );

resp.Write(endExcelXML);

resp.End();
}
}
}


你可能感兴趣的:(Excel)