asp.NetでDataTableデータをWordまたはExcelにエクスポートする方法を実現

4443 ワード

本文の例はaspを述べた.Netは、DataTableデータをWordまたはExcelにエクスポートする方法を実現します.皆さんの参考にしてください.具体的には以下の通りです.

/// < xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" />
///   DataTable   Word  Excel
/// 
/// Page  
/// DataTable   
///   Word  Excel     
///   Word  Excel        
///   Word  Excel  
///     (w:Word,e:Excel)
public bool DataTableToExcel(Page pPage, DataTable dt, string str_ExportTitle, string str_ExportContentTitle, string str_ExportMan, string str_ExportType)
{
    bool bl_Result = false;
    string str_ExportTypeName = "word";//    
    string str_ExportFormat = ".doc";//       
    if (str_ExportType.Equals("e"))
    {
      str_ExportTypeName = "excel";
      str_ExportFormat = ".xls";
    }
    HttpResponse response = pPage.Response;
    if (dt.Rows.Count > 0)
    {
      response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
      response.ContentType = "application/ms-" + str_ExportTypeName;
      response.AppendHeader("Content-Disposition", "attachment;filename="
      + HttpUtility.UrlEncode(str_ExportTitle, System.Text.Encoding.UTF8).ToString() //    ,         
      + str_ExportFormat);
      //  DataTable    
      int i_ColumnCount = dt.Columns.Count;
      //      DataTable  
      System.Text.StringBuilder builder = new System.Text.StringBuilder();
      builder.Append("
"); builder.Append("
"); builder.Append("
"); builder.Append("
"); builder.Append("
");
if (!string.IsNullOrEmpty(str_ExportContentTitle))
{
builder.Append(string.Concat(new object[] { ""}));
}
builder.Append("");
builder.Append("");
builder.Append("");
for (int i = 0; i < i_ColumnCount; i++)
{
if (dt.Columns[i].Caption.ToString().ToLower() != "id")
{
builder.Append("");
}
}
#regionここでは、エクスポートされたデータ列の一番前に1列(シーケンス列)を追加していません.
//エクスポートされたデータ列の一番前に列が1つも追加されていません(シーケンス列)
//foreach (DataRow row in dt.Rows)
//{
// builder.Append("");
// for (int j = 0; j < i_ColumnCount; j++)
// {
// if (dt.Columns[j].Caption.ToString().ToLower() != "id")
// {
// builder.Append("");
// }
// }
// builder.Append("");
//}
#endregion
#regionエクスポートされたデータ列の先頭にシーケンス番号列を付けた(注意:非DataTableデータのシーケンス番号)
//エクスポートされたデータ列の先頭にシーケンス番号列(注意:非DataTableデータのシーケンス番号)を付ける
for (int m = 0; m < dt.Rows.Count; m++)
{
builder.Append("");
for (int j = 0; j < i_ColumnCount; j++)
{
if (dt.Columns[j].Caption.ToString().ToLower() != "id")
{
if (j == 0)
{
builder.Append("");
}
if (j > 0)
{
builder.Append("");
}
if (j == dt.Columns.Count - 1)
{
builder.Append("");
}
}
}
builder.Append("");
}
#endregion
builder.Append("");
builder.Append("");
builder.Append("",
str_ExportContentTitle,
"");
builder.Append(「導出者:[」+str_ExportMan+")、導出時間:[」+DateTime.Now.ToString(「yyyy-MM-dd hh:mm:ss」)+「]シーケンス番号」+dt.Columns[i].Caption.ToString() + ""+ row[j].ToString() + ""+ (m + 1) + ""+ dt.Rows[m][j - 1].ToString() + ""+ dt.Rows[m][j].ToString() + "");
builder.Append("合計:合計"+dt.Rows.Count+")
response.Write(builder.ToString());
response.End();
bl_Result = true;
}
return bl_Result;
}
もっとaspについてNet関連内容に興味のある読者は、「asp.net操作jsonテクニックまとめ」、「asp.net文字列操作テクニックまとめ」、「asp.net操作XMLテクニックまとめ」、「asp.netファイル操作テクニックまとめ」、「asp.net ajaxテクニックまとめ」、「asp.netキャッシュ操作テクニックまとめ」を参照してください.
本文で述べたように皆さんにasp.Netプログラミングが役立ちます.