导出EXCEL的问题。 能不能将Grid导出到EXCEL的数据透视表 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用Excel的宏记录一下过程然后把宏生成的代码放到你的代码里执行即可 http://www.cnblogs.com/lovecherry/archive/2005/03/25/125519.html看看这个 using System;using System.IO;using Excel;namespace Openet.Utility.ExcelTools{ /// <summary> /// ExcelOption 的摘要说明。 /// </summary> public class ExcelTools { public ExcelTools() { } /// <summary> /// 导出到EXCEL表 /// </summary> public static void toExcel(string FileName, string SheetName, System.Data.DataTable dt) { Excel.ApplicationClass myExcel = new Excel.ApplicationClass(); Excel.Worksheet worksheet = null; string curPath = Directory.GetParent(FileName).FullName; if(!Directory.Exists(curPath)) { Directory.CreateDirectory(curPath); } if (File.Exists(FileName)) { try { File.Delete(FileName); } catch { throw new Exception("文件操作出错,可能正在运行当前的EXCEL文件!"); } } try { object missing = System.Reflection.Missing.Value; myExcel.Workbooks.Add(missing); worksheet = (Excel.Worksheet)myExcel.ActiveSheet; worksheet.Name = SheetName; myExcel.Visible = false; int iRows = dt.Rows.Count; int iCol = dt.Columns.Count; worksheet.get_Range( worksheet.Cells[1, 1], worksheet.Cells[ iRows+1, iCol] ).NumberFormatLocal = "@"; for( int i=0; i<iCol; i++) { worksheet.Cells[1,i+1] = dt.Columns[i].ToString(); } int size = iRows / 1000; int page = 0; string[,] rows = null; for(int i=0; i<iRows; ) { if(page<size) { rows = new string[1000,iCol]; for(int count=0;count<1000;count++) { for(int j=0; j<iCol; j++) { rows[count,j] = dt.DefaultView[i][j].ToString(); } i++; } worksheet.get_Range(worksheet.Cells[page * 1000 + 2, 1], worksheet.Cells[ page * 1000 + 1001, iCol]).Value2 = rows; page++; } else { int left = iRows % 1000; rows = new string[left,iCol]; for(int count=0;count<left;count++) { for(int j=0; j<iCol; j++) { rows[count,j] = dt.DefaultView[i][j].ToString(); } i++; } worksheet.get_Range(worksheet.Cells[iRows+2-left, 1], worksheet.Cells[ iRows+1, iCol]).Value2 = rows; } } worksheet.SaveAs( FileName, missing, missing, missing, missing, missing, missing, missing, missing, missing); } catch(Exception ex) { throw new Exception(ex.Message); } finally { myExcel.Workbooks.Close(); myExcel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel); worksheet = null; myExcel = null; GC.Collect(); } } /// <summary> /// 因为调用EXCEL后,结束时EXCEL进程并没有立即KILL,用这个方法调用可以KILL掉EXCEL /// </summary> public static void callToExcel(string FileName, string SheetName, System.Data.DataTable dt) { toExcel(FileName, SheetName, dt); GC.Collect(); } }} wpf的资 我想用C#截取桌面图片(要包含鼠标在内),该怎么做? JAVA 和C# 的区别 请问一下,C#游戏测试工程师,一般会做些什么? 读xml文件,相同的节点怎么读出来?在线等 关于.NET的XML Serialization(序列化)问题,在线等,当日送分! 高人在吗?????在线等????? 请问table的合并行、列的问题 转C# 一个奇怪的问题,我的Keydown事件为什么只能响应一次? 一个窗口重载问题.... 怎么才能让C#程序在Vista下面出现UAC标记!
然后把宏生成的代码放到你的代码里执行即可
using System.IO;
using Excel;namespace Openet.Utility.ExcelTools
{
/// <summary>
/// ExcelOption 的摘要说明。
/// </summary>
public class ExcelTools
{
public ExcelTools()
{ } /// <summary>
/// 导出到EXCEL表
/// </summary>
public static void toExcel(string FileName, string SheetName, System.Data.DataTable dt)
{
Excel.ApplicationClass myExcel = new Excel.ApplicationClass();
Excel.Worksheet worksheet = null; string curPath = Directory.GetParent(FileName).FullName;
if(!Directory.Exists(curPath))
{
Directory.CreateDirectory(curPath);
} if (File.Exists(FileName))
{
try
{
File.Delete(FileName);
}
catch
{
throw new Exception("文件操作出错,可能正在运行当前的EXCEL文件!");
}
} try
{
object missing = System.Reflection.Missing.Value; myExcel.Workbooks.Add(missing);
worksheet = (Excel.Worksheet)myExcel.ActiveSheet;
worksheet.Name = SheetName;
myExcel.Visible = false;
int iRows = dt.Rows.Count;
int iCol = dt.Columns.Count; worksheet.get_Range( worksheet.Cells[1, 1], worksheet.Cells[ iRows+1, iCol] ).NumberFormatLocal = "@"; for( int i=0; i<iCol; i++)
{
worksheet.Cells[1,i+1] = dt.Columns[i].ToString();
} int size = iRows / 1000;
int page = 0;
string[,] rows = null;
for(int i=0; i<iRows; )
{
if(page<size)
{
rows = new string[1000,iCol];
for(int count=0;count<1000;count++)
{
for(int j=0; j<iCol; j++)
{
rows[count,j] = dt.DefaultView[i][j].ToString();
}
i++;
}
worksheet.get_Range(worksheet.Cells[page * 1000 + 2, 1], worksheet.Cells[ page * 1000 + 1001, iCol]).Value2 = rows;
page++;
}
else
{
int left = iRows % 1000;
rows = new string[left,iCol];
for(int count=0;count<left;count++)
{
for(int j=0; j<iCol; j++)
{
rows[count,j] = dt.DefaultView[i][j].ToString();
}
i++;
}
worksheet.get_Range(worksheet.Cells[iRows+2-left, 1], worksheet.Cells[ iRows+1, iCol]).Value2 = rows;
}
}
worksheet.SaveAs( FileName, missing, missing, missing, missing, missing, missing, missing, missing, missing); }
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
myExcel.Workbooks.Close();
myExcel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
worksheet = null;
myExcel = null;
GC.Collect();
}
} /// <summary>
/// 因为调用EXCEL后,结束时EXCEL进程并没有立即KILL,用这个方法调用可以KILL掉EXCEL
/// </summary>
public static void callToExcel(string FileName, string SheetName, System.Data.DataTable dt)
{
toExcel(FileName, SheetName, dt);
GC.Collect();
}
}}