using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace Utility.Util

{

public class Excelcs
#region
public class Excelcs

{
public static bool DataGridviewShowToExcel(DataTable dt,
bool isShowExcle)

{
if (dt.Rows.Count == 0)
return false;
//建¡§立¢¡éExcel对?象¨® 
Microsoft.Office.Interop.Excel.Application excel =
new Microsoft.Office.Interop.Excel.Application();

excel.Application.Workbooks.Add(
true);

excel.Visible = isShowExcle;
//生¦¨²成¨¦字Á?段?名?称?
for (
int i = 0; i < dt.Columns.Count; i++)

{

excel.Cells[1, i + 1] = dt.Columns[i].Caption;

}
//填¬?充?数ºy据Y
for (
int i = 0; i < dt.Rows.Count - 1; i++)

{
for (
int j = 0; j < dt.Columns.Count; j++)

{
if (dt.Rows[j][i].GetType() ==
typeof(
string))

{

excel.Cells[i + 2, j + 1] =
"'" + dt.Rows[j][i].ToString();

}
else 
{

excel.Cells[i + 2, j + 1] = dt.Rows[j][i].ToString();

}

}

}
return true;

}

}

#endregion

}