作者:乔山办公网日期:
返回目录:excel表格制作
office2003主户百操作集
Microsoft Visual Studio 2005 Tools for Office Second Edition 运行度库
安好后Region、知cell、row等对象可以供道你调用来回实现你的答功能
首先,需要在excel中添加加来载项--数据分析库,然后就可以进行数据自动生成了,以专业的术语叫做“随机数发生器”。依次点击:excel选项-加载项-转到,进行分析工具库的添加工作。
分析工具库添加完成之后,在源excel的“数据”选项卡上,最右侧会多出一个“分析”的菜单栏,点击“数据分析”。
选择“数据分析”下的“随机数发生器”。
弹出的界百面上,为随机数发生器的参数设置界面。其中,变量个度数=生成数据列数、随机数个数=生成数据行数,比如,设置:变量个数=5、随机数个数=10,那么,就会生成一个5列、10行的数据。
接下来,选择随机数据的分布类型,以问“正态分布”为例,设定:平均值=50、标准差=5。
选定数据的输出位置,可以选定区域答、新建工作表、新建工作簿,本例中以选定区域为例来说情况,如下图,选择将数据输出到:A1:E10区域中。
数据输出成功,如下图所示。
用npoi就能实现.
using System;
using System.IO;
using Excel;
namespace Kits.Excel
{
public class ExcelTools
{
/// <summary>
/// 导出到EXCEL表
/// </summary>
private static void toExcel(string FileName, string SheetName, System.Data.DataTable dt)
{
ApplicationClass myExcel = new ApplicationClass();
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 = (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();
}
}
}
需要添加一个引e68a84e799bee5baa6e997aee7ad94333用Excel