求.net2005中windows应用程序将datatable转成Excel文件的代码(急)
求.net2005中windows应用程序将datatable转成Excel文件的代码,windows应用程序,将datatable转换成Excel,要2005的,谢谢~ --------------------编程问答-------------------- 这是Datatable到Excel的方法.你可以参考一下.稍微改动一下应该就可以了.
private void DataTableToFile(DataTable dt, string strFileName)
{
Excel.Application app = null;
Excel.Workbook book = null;
Excel.Worksheet sheet = null;
Excel.Range rng = null;
bool bExp = false;
try
{
app = new Excel.Application();
app.DisplayAlerts = false;
app.Visible = false;
int nSheetIdx = 1;
book = app.Workbooks.Add(Missing.Value);
///列数
int nColumnCnt = 0;
///行数
int nRowCnt = 0;
///当前列索引
int nColumnIdx = 0;
///当前行索引
int nRowIdx = 0;
///Sheet表的个数
int nSheetCnt = 0; //Sheet Number of Same Table
int nSheetRowCnt = 0; //Sheet Row Count
int nSheetRowIdx = 0; //Sheet Row Index
string strValue = "";
nColumnCnt = dt.Columns.Count;
if (nColumnCnt > 255) nColumnCnt = 255;
nRowCnt = dt.Rows.Count;
if (nRowCnt % MAX_EXCEL_ROW == 0)
nSheetCnt = nRowCnt / MAX_EXCEL_ROW;
else
nSheetCnt = nRowCnt / MAX_EXCEL_ROW + 1;
string[,] strColumns = new string[1, nColumnCnt];
//把列标题存放在数组中。
for (nColumnIdx = 0; nColumnIdx < nColumnCnt; nColumnIdx++)
strColumns[0, nColumnIdx] = dt.Columns[nColumnIdx].ColumnName;
for (int nTableSheetIdx = 0; nTableSheetIdx < nSheetCnt; nTableSheetIdx++)
{
if (nSheetIdx > book.Worksheets.Count)
book.Worksheets.Add(Missing.Value, book.Worksheets.get_Item(nSheetIdx - 1), 1, Missing.Value);
sheet = (Excel.Worksheet)book.Worksheets.get_Item(nSheetIdx);
if (nTableSheetIdx == 0)
sheet.Name = dt.TableName;
else
sheet.Name = dt.TableName + "_" + nTableSheetIdx.ToString();
nSheetIdx++;
//// 报告的标题
rng = sheet.get_Range("A1", Missing.Value);
rng = rng.get_Resize(1, nColumnCnt);
rng.Font.Bold = true;
rng.set_Value(Missing.Value, strColumns);
rng.EntireColumn.AutoFit();
if (nRowCnt == 0) continue;
rng = sheet.get_Range("A2", Missing.Value);
if (nTableSheetIdx == 0)
{
if (nRowCnt > MAX_EXCEL_ROW)
nSheetRowCnt = MAX_EXCEL_ROW;
else
nSheetRowCnt = nRowCnt;
}
else if (nTableSheetIdx < (nSheetCnt - 1))
nSheetRowCnt = MAX_EXCEL_ROW;
else
nSheetRowCnt = nRowCnt - nTableSheetIdx * MAX_EXCEL_ROW;
//获得range的区域
rng = rng.get_Resize(nSheetRowCnt, nColumnCnt);
//定义对象数组,用来存放从数据库中取出来的数。最终写到Excel文件中。
object[,] objValues = new object[nSheetRowCnt, nColumnCnt];
for (nRowIdx = 0 + nTableSheetIdx * MAX_EXCEL_ROW; nRowIdx < (1 + nTableSheetIdx) * MAX_EXCEL_ROW; nRowIdx++)
{
if (nRowIdx == nRowCnt) break;
nSheetRowIdx = nRowIdx - nTableSheetIdx * MAX_EXCEL_ROW;
for (nColumnIdx = 0; nColumnIdx < nColumnCnt; nColumnIdx++)
{
if (dt.Rows[nRowIdx][nColumnIdx] != System.DBNull.Value)
{
strValue = dt.Rows[nRowIdx][nColumnIdx].ToString();
objValues[nSheetRowIdx, nColumnIdx] = strValue;
}
}
}
rng.set_Value(Missing.Value, objValues);
}
}
catch (System.Exception ex)
{
bExp = true;
throw ex;
}
finally
{
if (book != null)
{
if (bExp)
book.Close(false, Missing.Value, Missing.Value);
else
book.SaveAs(strFileName, Excel.XlFileFormat.xlExcel9795, Missing.Value, Missing.Value, false,
false, Excel.XlSaveAsAccessMode.xlNoChange,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
}
if (app != null)
{
app.Quit();
app = null;
}
}
} --------------------编程问答-------------------- app = new Excel.Application();无法创建抽象类或接口的实例 --------------------编程问答-------------------- 要添加引用EXCEL的COM组件。 --------------------编程问答-------------------- 帮顶 ... --------------------编程问答-------------------- 我已经添加了vbide,off和excel的组件呀,还是一样的
--------------------编程问答-------------------- 我引入了命名空间
using Microsoft.Office.Interop.Excel;
using ExcelApplication = Microsoft.Office.Interop.Excel.Application;
由于我想把datatiable转换成Excel这样在方法的datatable参数的时候
public void ExportExcel(DataTable dt)这个DataTable出现了
“DataTable”是“System.Data.DataTable”和“Microsoft.Office.Interop.Excel.DataTable”之间的不明确的引用错误 --------------------编程问答-------------------- 问题还是没有解决呢
顶!~~ --------------------编程问答-------------------- Missing.Value 和MAX_EXCEL_ROW是什么意思 啊,能解释一下吗?谢谢 --------------------编程问答-------------------- 救急了,兄弟姐妹们,我现在想要从Datatable导出数据到Excel,哪位有相关的代码奉献一把吧 --------------------编程问答-------------------- 你将public void ExportExcel(DataTable dt) 改为public void ExportExcel( System.Data.DataTable dt) 就不会出现不明确的引用错误提示了。 --------------------编程问答-------------------- 使用Missing.Value,需要引用using System.Reflection;
是调用Excel.com组件的一种写法.
MAX_EXCEL_ROW是我里面定义的Excel最大的行数,定义的一个字符常量.65535. --------------------编程问答-------------------- 顶!+学习+接分! --------------------编程问答-------------------- http://expert.csdn.net/Expert/topic/1146/1146751.xml?temp=.3858759 --------------------编程问答-------------------- 帮顶,我也急着需要这块东西 --------------------编程问答-------------------- 看看它到底用处多大~~~ --------------------编程问答-------------------- 把datatable绑定到gridview再导出
#region 导出为Excel
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
private void ToExcel(Control ctl, string FileName)
{
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
private void toExcelClk()
{
gvSysUser.DataSource = datatable;
gvSysUser.DataBind();
ToExcel(gvSysUser, "OFS_Data.xls");
}
#endregion
补充:.NET技术 , C#