如何将DataTable写入到一个Excel中 经过一系列的查询后,得到一个DataTable。接下来如何将这个表写入到Excel中。谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://blog.csdn.net/blackhero/archive/2006/08/25/1116399.aspx http://www.chinabs.net/webdotnet/default.asp?infoid=417 //*****建立一个Windows 项目 //*****添加引用类 microsoft.offices excel.90 //*****引入命名空间 using system.data; using system.data.sqlclient; //******导出数据到Excel public static void OutputExcel() { ///*****添加一个表薄 Excle.application appExcel=new excel.application(); Excel.workbook xbook=new Excle.workbook(); Excel.worksheet xsheet=new Excel.worksheet(); //****添加一个工作薄 xsheet=appExcle.worksheets.add("sheet"); DataColumn col; int col=0;//******显示行 int row=1;//******显示列 //*****打开Excel程序 appExcel.visible=true; //***获取数据源 DataTable dTable=new DataTable(); dTable=GetTable(); //*****导出数据 foreach(DataColumn col in dTable.Columname) { col+=1; row=1; foreach(datarow row in dTable.rows) { row+=1; col=1; appExcle.cell(row,col)=row(col.columname); } } public static int ExportToExcel(DataSet ds, string filename) { int state = 0; int tablecount = ds.Tables.Count; if (tablecount <= 0) return 0; int index = 0; Excel.Application xlApp = null; Excel.Workbook xlBook = null; Excel.Worksheet xlSheet = null; int rowIndex = 1; int colIndex = 0; object missing = Missing.Value; List<System.Data.DataTable> listTable = new List<System.Data.DataTable>(); try { xlApp = new Excel.Application(); xlBook = xlApp.Workbooks.Add(true); foreach (System.Data.DataTable dt in ds.Tables) { index++; rowIndex = 1; colIndex = 0; xlSheet = (Excel.Worksheet)xlApp.Worksheets[1]; xlSheet.Name = dt.TableName; foreach (DataColumn Col in dt.Columns) { colIndex = colIndex + 1; xlApp.Cells[1, colIndex] = " " + Col.ColumnName; } //foreach (DataRow Row in dt.Rows) //{ // rowIndex = rowIndex + 1; // colIndex = 0; // foreach (DataColumn Col in dt.Columns) // { // colIndex = colIndex + 1; // xlApp.Cells[rowIndex, colIndex] = Row.ItemArray[colIndex - 1]; // } //} int rowNum = dt.Rows.Count; int colNum = dt.Columns.Count; string[,] finalData = new string[rowNum, colNum]; for (int i = 0; i < rowNum; i++) { for (int j = 0; j < colNum; j++) { finalData[i, j] = dt.Rows[i][j].ToString(); } } xlSheet.get_Range(xlSheet.Cells[1, 1], xlSheet.Cells[1, colIndex]).Font.Bold = true; xlSheet.get_Range(xlSheet.Cells[1, 1], xlSheet.Cells[rowIndex, colIndex]).Borders.LineStyle = 1; xlSheet.get_Range(xlSheet.Cells[2, 1], xlSheet.Cells[rowNum + 1, colNum]).Value2 = finalData; xlSheet.get_Range(xlSheet.Cells[2, 1], xlSheet.Cells[rowNum + 1, colNum]).NumberFormatLocal = "@"; if (index < tablecount) xlSheet = (Excel.Worksheet)xlApp.Worksheets.Add(missing, missing, missing, missing); } if (System.IO.File.Exists(filename)) System.IO.File.Delete(filename); xlApp.Visible = false; xlSheet.SaveAs(filename, missing, missing, missing, missing, missing, missing, missing, missing, missing); state = 1; } finally { object saveChange = true; xlBook.Close(saveChange, filename, missing); xlApp.Quit(); if(xlSheet!=null) Marshal.ReleaseComObject(xlSheet); if (xlBook != null) Marshal.ReleaseComObject(xlBook); if (xlApp != null) Marshal.ReleaseComObject(xlApp); xlApp = null; xlBook = null; xlSheet = null; GC.Collect(); } return state; } 贪吃蛇的程序,蛇吃到食物的时候程序就运行不了,请帮忙修改下 求教大神,自定义控件的问题 如果一个类的代码不多,也要独占一个cs文件吗? 一个简单问题 C#里可有像VB里面那样的确定取消对话框? 來看看老朋友﹐順便帶上几個問題為難一下以表心意﹐祝大家十一節日快樂﹗﹗﹗﹗﹗﹗﹗﹗﹗哇哈哈哈。。。。 向各位高手请教:如何将progressBar与数据库查询联系起来? 关于.net线程的问题 DataSet1.Tables.Add(); 如何判断两台机子是否在同一局域网?真急啊! C#问题,请各位帮我找一下错误,提示未将对象引用设置到对象实例!散分! 请教一个物流方面的问题!(家九)请进!
//*****添加引用类 microsoft.offices excel.90
//*****引入命名空间
using system.data;
using system.data.sqlclient;
//******导出数据到Excel
public static void OutputExcel()
{
///*****添加一个表薄
Excle.application appExcel=new excel.application();
Excel.workbook xbook=new Excle.workbook();
Excel.worksheet xsheet=new Excel.worksheet();
//****添加一个工作薄
xsheet=appExcle.worksheets.add("sheet");
DataColumn col;
int col=0;//******显示行
int row=1;//******显示列
//*****打开Excel程序
appExcel.visible=true;
//***获取数据源
DataTable dTable=new DataTable();
dTable=GetTable();
//*****导出数据
foreach(DataColumn col in dTable.Columname)
{
col+=1;
row=1;
foreach(datarow row in dTable.rows)
{
row+=1;
col=1;
appExcle.cell(row,col)=row(col.columname);
}
}
{
int state = 0;
int tablecount = ds.Tables.Count;
if (tablecount <= 0) return 0;
int index = 0;
Excel.Application xlApp = null;
Excel.Workbook xlBook = null;
Excel.Worksheet xlSheet = null;
int rowIndex = 1;
int colIndex = 0;
object missing = Missing.Value; List<System.Data.DataTable> listTable = new List<System.Data.DataTable>();
try
{
xlApp = new Excel.Application(); xlBook = xlApp.Workbooks.Add(true); foreach (System.Data.DataTable dt in ds.Tables)
{
index++;
rowIndex = 1;
colIndex = 0; xlSheet = (Excel.Worksheet)xlApp.Worksheets[1]; xlSheet.Name = dt.TableName; foreach (DataColumn Col in dt.Columns)
{
colIndex = colIndex + 1;
xlApp.Cells[1, colIndex] = " " + Col.ColumnName;
} //foreach (DataRow Row in dt.Rows)
//{
// rowIndex = rowIndex + 1; // colIndex = 0;
// foreach (DataColumn Col in dt.Columns)
// {
// colIndex = colIndex + 1;
// xlApp.Cells[rowIndex, colIndex] = Row.ItemArray[colIndex - 1];
// }
//} int rowNum = dt.Rows.Count;
int colNum = dt.Columns.Count;
string[,] finalData = new string[rowNum, colNum];
for (int i = 0; i < rowNum; i++)
{
for (int j = 0; j < colNum; j++)
{
finalData[i, j] = dt.Rows[i][j].ToString();
}
}
xlSheet.get_Range(xlSheet.Cells[1, 1], xlSheet.Cells[1, colIndex]).Font.Bold = true;
xlSheet.get_Range(xlSheet.Cells[1, 1], xlSheet.Cells[rowIndex, colIndex]).Borders.LineStyle = 1;
xlSheet.get_Range(xlSheet.Cells[2, 1], xlSheet.Cells[rowNum + 1, colNum]).Value2 = finalData;
xlSheet.get_Range(xlSheet.Cells[2, 1], xlSheet.Cells[rowNum + 1, colNum]).NumberFormatLocal = "@"; if (index < tablecount) xlSheet = (Excel.Worksheet)xlApp.Worksheets.Add(missing, missing, missing, missing);
} if (System.IO.File.Exists(filename)) System.IO.File.Delete(filename);
xlApp.Visible = false;
xlSheet.SaveAs(filename, missing, missing, missing, missing, missing, missing, missing, missing, missing);
state = 1;
}
finally
{
object saveChange = true;
xlBook.Close(saveChange, filename, missing);
xlApp.Quit();
if(xlSheet!=null) Marshal.ReleaseComObject(xlSheet);
if (xlBook != null) Marshal.ReleaseComObject(xlBook);
if (xlApp != null) Marshal.ReleaseComObject(xlApp);
xlApp = null;
xlBook = null;
xlSheet = null;
GC.Collect();
}
return state;
}