乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 用c#.net创建一个Excel文件

用c#.net创建一个Excel文件

作者:乔山办公网日期:

返回目录:excel表格制作


#region 从DataTable读取内容来创建Workbook对象zd: public static MemoryStream RenderToExcel(DataTable table) { MemoryStream ms = new MemoryStream(); using (table) { IWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook

1. 创建一个新Excel的Application:

Application exc = new Application();
if (exc == null) {
Console.WriteLine("ERROR: EXCEL couldn't be started");
return 0;
}

2. 让这个工程可见:

exc.set_Visible(0, true);

3. 获取WorkBooks集合:

Workbooks workbooks = exc.Workbooks;

4. 加入新的WorkBook:

_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);

5. 获取WorkSheets集合:

_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
if (worksheet == null) {
Console.WriteLine ("ERROR in worksheet == null");
}

6. 给单元格设置变量:

Range range1 = worksheet.get_Range("C1", Missing.Value);
if (range1 == null) {
Console.WriteLine ("ERROR: range == null");
}
const int nCells = 1;
Object[] args1 = new Object[1];
args1[0] = nCells;
range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range1, args1);

例程:

using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Excel;

class Excel {
public static int Main() {
Application exc = new Application();
if (exc == null) {
Console.WriteLine("ERROR: EXCEL couldn't be started!");
return 0;
}

exc.set_Visible(0, true);
Workbooks workbooks = exc.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);
Sheets sheets = workbook.Worksheets;

_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
if (worksheet == null) {
Console.WriteLine ("ERROR: worksheet == null");
}

Range range1 = worksheet.get_Range("C1", Missing.Value);
if (range1 == null) {
Console.WriteLine ("ERROR: range == null");
}
const int nCells = 1;
Object[] args1 = new Object[1];
args1[0] = nCells;
range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null,range1, args1);
return 100;
}
}

现在我们来看看如何使用数组,他有些类似于设置单元格。仅仅需要的改变只7a686964616fe58685e5aeb9335是args2[0] = array2;
const int nCell = 5;
Range range2 = worksheet.get_Range("A1", "E1");
int[] array2 = new int [nCell];
for (int i=0; i < array2.GetLength(0); i++) {
array2[i] = i+1;
}
Object[] args2 = new Object[1];
args2[0] = array2;
range2.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range2, args2);
protected void ButSave_Click(object sender, EventArgs e)
{
sendTableName = "Phone, Illness, See_Illness,Leechdom,Notes";
sendStrSQL = this.Label8.Text;
dataBase();
DataView dv = new DataView(ds.Tables[0]);
OutputExcel(dv,"塔里木石油医院慢性病用药报表");
}
public void OutputExcel(DataView dv, string str)
{
//
// TODO: 在此处添加构造函数逻辑
//
//dv为要输出到Excel的数据,str为标题名称
GC.Collect();
//Application excel;// = new Application();
int rowIndex = 2;
int colIndex = 0;
int SUM = 0;
int number = 0;
_Workbook xBk;
_Worksheet xSt;

Excel.ApplicationClass excel = new Excel.ApplicationClass();
xBk = excel.Workbooks.Add(true);

xSt = (_Worksheet)xBk.ActiveSheet;

//
//取得标题
//
foreach (DataColumn col in dv.Table.Columns)
{
colIndex++;
excel.Cells[2, colIndex] = col.ColumnName;
xSt.get_Range(excel.Cells[2, colIndex], excel.Cells[2, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
}

//
//取得表格中的数据
//
foreach (DataRowView row in dv)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in dv.Table.Columns)
{
colIndex++;
//if (col.DataType == System.Type.GetType("System.DateTime"))
//{
// excel.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 = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
//}
//else
if (col.DataType == System.Type.GetType("System.String"))
{

excel.Cells[rowIndex, colIndex] = "'" + row[col.ColumnName].ToString();

xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
if (col.ColumnName.ToString() == "用药数量")
{
number = colIndex;
}
}
}
}
//
//加载一个合计行
//
int rowSum = rowIndex + 1;
int colSum = 1;
if (number != 0)
{
excel.Cells[rowSum, number] = this.lblnumber.Text;
}
excel.Cells[rowSum, 1] = "合计";
xSt.get_Range(excel.Cells[rowSum, 1], excel.Cells[rowSum, 1]).HorizontalAlignment = XlHAlign.xlHAlignCenter;

//if (row[col.ColumnName].ToString() = "用药数量")
//{

//}
//
//设置选中的部分的颜色
//
xSt.get_Range(excel.Cells[rowSum, colSum], excel.Cells[rowSum, colIndex]).Select();
xSt.get_Range(excel.Cells[rowSum, colSum], excel.Cells[rowSum, colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种
//
//取得整个报表的标题
//
excel.Cells[1, 1] = str;
//
//设置整个报表的标题格式
//
//xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Font.Bold = true;
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Font.Size = 16;
//
//设置报表表格为最适应宽度
//
xSt.get_Range(excel.Cells[2, 1], excel.Cells[rowSum, colIndex]).Select();
xSt.get_Range(excel.Cells[2, 1], excel.Cells[rowSum, colIndex]).Columns.AutoFit();
//
//设置整个报表的标题为跨列居中
//
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, colIndex]).Select();
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
//
//绘制边框
//
xSt.get_Range(excel.Cells[2, 1], excel.Cells[rowSum, colIndex]).Borders.LineStyle = 1;
xSt.get_Range(excel.Cells[2, 1], excel.Cells[rowSum, 1]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗
xSt.get_Range(excel.Cells[2, 1], excel.Cells[2, colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置上边线加粗
xSt.get_Range(excel.Cells[2, colIndex], excel.Cells[rowSum, colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗
xSt.get_Range(excel.Cells[rowSum, 1], excel.Cells[rowSum, colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗
//
//显示效果
//
excel.Visible = true;

xBk.SaveCopyAs(Server.MapPath(".") + "\\" + "2008.xls");

ds = null;
xBk.Close(false, null, null);

excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
GC.Collect();
string path = Server.MapPath("2008.xls");

System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());

// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";

// 把文件流发送到636f7079e799bee5baa6332客户端
Response.WriteFile(file.FullName);
// 停止页面的执行

Response.End();

}
另外,站长团上有产品团购,便宜有保证

没装office也得有dll啊,
要不然没有办法。你没有类库怎么操作啊。

相关阅读

  • 用c#.net创建一个Excel文件

  • 乔山办公网excel表格制作
  • #region 从DataTable读取来内容来创自建Workbook对象zd: public static MemoryStream RenderToExcel(DataTable table) { MemoryStream ms = new MemoryStream(); using (tabl
  • c#<em>winform</em>怎么向<em>excel</em>中

  • 乔山办公网excel表格制作
  • 这个问题我来也遇到 了,纠结了好久,office2003用WEBBROWSER 这个方法可以,可2007不行,不过我最后找到办法解决了,理解寻找解决办法人的心情,所以这里贡献出来给大家。DsoFramer_KB31
  • C#读取Excel文件的几种方法

  • 乔山办公网excel表格制作
  • oledb操作Excel的几个函数" src="/uploads/tu/139.jpg" style="width: 400px; height: 267px;" />使用第三方dll吧,比如npoi//将Excel表格中某7a64e59b9ee7ad94366一sheet中内容放于dataGrid
  • -winform操作excel,winform怎么获取excel

  • 乔山办公网excel表格制作
  • 出现这个知错误的原因是这台电脑上没有注册该COM组件。请尝道试以下方法:运行excel.exe /regserver卸载重装office此外,请内确认生成程序使用的COM组件对应的Office版本与运行程序的系统
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网