C#操作excel表问题。 1.单击butten在指定目录下创建excel表。(若路径不存在自动创建)2.如何设置表头。3.如何向指定坐标写入数据。4.如何关闭表 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://blog.csdn.net/simonllf/archive/2006/12/13/1441672.aspx看看高手写的吧。。我只是转贴!~ 贴个简单的,用到Excel.dll#region (使用模版)将DataTable的数据导出显示为报表 /// <summary> /// 将DataTable的数据导出显示为报表 /// </summary> /// <param name="dt">要导出的数据</param> /// <param name="FilePath">保存文件的路径</param> /// <param name="SheetsNumber">导出报表的Sheet数</param> /// <param name="RowIndex">导出报表的起始行</param> /// <param name="ColIndex">导出报表的起始列</param> /// <param name="TemplateFile">模版文件</param> /// <returns></returns> public string OutputExcel(System.Data.DataTable dt, string FilePath, int SheetsNumber,int RowIndex, int ColIndex, string TemplateFile) { Excel.Application excel; Excel._Workbook xBk; Excel._Worksheet xSt = null; excel = new Excel.ApplicationClass(); xBk = excel.Workbooks._Open(@TemplateFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); for (int i = 0; i < SheetsNumber; i++) { int index = i + 1; int rowIndex = RowIndex; xSt = (Excel.Worksheet)xBk.Worksheets.get_Item(index); //取得表格中的数据 foreach (DataRow row in dt.Rows) { int colIndex = ColIndex; rowIndex++; foreach (DataColumn col in dt.Columns) { colIndex++; if (col.DataType == System.Type.GetType("System.DateTime")) { xSt.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd"); //xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐 } else if (col.DataType == System.Type.GetType("System.String")) { xSt.Cells[rowIndex, colIndex] = row[col.ColumnName]; //xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐 } else { xSt.Cells[rowIndex, colIndex] = row[col.ColumnName]; } } } } string filename = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xls"; excel.ActiveWorkbook.SaveAs(FilePath + filename, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null); #region 结束Excel进程 xBk.Close(null, null, null); excel.Workbooks.Close(); excel.Quit(); //注意:这里用到的所有Excel对象都要执行这个操作,否则结束不了Excel进程 if (xSt != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt); xSt = null; } if (xBk != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk); xBk = null; } if (excel != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(excel); excel = null; } GC.Collect();//垃圾回收 #endregion } string.Empty和""一样吗 请教:如何解除项目版本依赖 C#中计算gridview某一列的和 异步处理方法中事件响应方法的执行问题? 小说阅读器如何增加书签? datagrid(Web Form)如何让数据居中显示? 问下哪里有程序设计师或高级程序设计师的试题啊,想考张证 怎么知道当前系统盘是那个盘符?加急!!! 非常急了,要把DataSet导出为Excel 水晶报表在阅览与打印时总是位置不正确!只有在阅览工具条.... 引用强名称程序集microsoft.mshtm.dll时事件调用不了? 关于暑假作业的问题三(项目)(80分求)
/// <summary>
/// 将DataTable的数据导出显示为报表
/// </summary>
/// <param name="dt">要导出的数据</param>
/// <param name="FilePath">保存文件的路径</param>
/// <param name="SheetsNumber">导出报表的Sheet数</param>
/// <param name="RowIndex">导出报表的起始行</param>
/// <param name="ColIndex">导出报表的起始列</param>
/// <param name="TemplateFile">模版文件</param>
/// <returns></returns>
public string OutputExcel(System.Data.DataTable dt, string FilePath, int SheetsNumber,int RowIndex, int ColIndex, string TemplateFile)
{
Excel.Application excel;
Excel._Workbook xBk;
Excel._Worksheet xSt = null; excel = new Excel.ApplicationClass();
xBk = excel.Workbooks._Open(@TemplateFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
for (int i = 0; i < SheetsNumber; i++)
{
int index = i + 1;
int rowIndex = RowIndex;
xSt = (Excel.Worksheet)xBk.Worksheets.get_Item(index); //取得表格中的数据
foreach (DataRow row in dt.Rows)
{
int colIndex = ColIndex;
rowIndex++;
foreach (DataColumn col in dt.Columns)
{
colIndex++;
if (col.DataType == System.Type.GetType("System.DateTime"))
{
xSt.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
//xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
}
else
if (col.DataType == System.Type.GetType("System.String"))
{
xSt.Cells[rowIndex, colIndex] = row[col.ColumnName];
//xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
xSt.Cells[rowIndex, colIndex] = row[col.ColumnName];
}
}
}
} string filename = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xls";
excel.ActiveWorkbook.SaveAs(FilePath + filename, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
#region 结束Excel进程
xBk.Close(null, null, null);
excel.Workbooks.Close();
excel.Quit(); //注意:这里用到的所有Excel对象都要执行这个操作,否则结束不了Excel进程
if (xSt != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xSt = null;
}
if (xBk != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
xBk = null;
}
if (excel != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
excel = null;
}
GC.Collect();//垃圾回收
#endregion
}