问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

asp.net 制作Excel报表,并应用。

发布网友 发布时间:2022-04-28 19:44

我来回答

1个回答

热心网友 时间:2022-04-27 13:11

// --- 源码如下,有说明。
using System;
using System.Web.Hosting;
using Excel = Microsoft.Office.Interop.Excel;

namespace InfoMS.OfficeUtility
{
public class Excel2003
{
public string readPath;
public string writePath;
public string mFilename;
public Excel.Application app;
public Excel.Workbooks wbs;
public Excel.Workbook wb;
public Excel.Worksheets wss;
public Excel.Worksheet ws;

public Excel2003(string readpath, string writepath)
{
this.readPath = readpath;
this.writePath = writepath;
}

public void Create()//创建一个Excel对象
{
app = new Excel.Application();
wbs = app.Workbooks;
wb = wbs.Add(true);
}

public void Open(string FileName)//打开一个Excel文件
{
app = new Excel.Application();
wbs = app.Workbooks;
wb = wbs.Add(readPath + FileName);
//wb = wbs.Open(FileName, 0, true, 5,"", "", true, Excel.XlPlatform.xlWindows, "t", false, false, 0, true,Type.Missing,Type.Missing);
//wb = wbs.Open(FileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Excel.XlPlatform.xlWindows,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
mFilename = FileName;
}

public Excel.Worksheet GetSheet(string SheetName) //获取一个工作表
{
Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets[SheetName];
return s;
}

public Excel.Worksheet AddSheet(string SheetName) //添加一个工作表
{
Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets.Add(Type.Missing,Type.Missing,Type.Missing,Type.Missing);
s.Name = SheetName;
return s;
}

public void DelSheet(string SheetName)//删除一个工作表
{
((Excel.Worksheet)wb.Worksheets[SheetName]).Delete();
}

public Excel.Worksheet ReNameSheet(string OldSheetName, string NewSheetName)//重命名一个工作表一
{
Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets[OldSheetName];
s.Name = NewSheetName;
return s;
}

public Excel.Worksheet ReNameSheet(Excel.Worksheet Sheet, string NewSheetName)//重命名一个工作表二
{

Sheet.Name = NewSheetName;

return Sheet;
}

public void SetCellValue(Excel.Worksheet ws, int x, int y, object value) //ws:要设值的工作表 X行Y列 value 值
{
ws.Cells[x, y] = value;
}

public void SetCellValue(string ws, int x, int y, object value) //ws:要设值的工作表的名称 X行Y列 value 值
{

GetSheet(ws).Cells[x, y] = value;
}

//设置一个单元格的属性 字体, 大小,颜色 ,对齐方式
public void SetCellProperty(Excel.Worksheet ws, int Startx, int Starty, int Endx, int Endy, int size, string name, Excel.Constants color, Excel.Constants HorizontalAlignment)
{
name = "宋体";
size = 12;
color = Excel.Constants.xlAutomatic;
HorizontalAlignment = Excel.Constants.xlRight;
ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name;
ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;
ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;
ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;
}

public void SetCellProperty(string wsn, int Startx, int Starty, int Endx, int Endy, int size, string name, Excel.Constants color, Excel.Constants HorizontalAlignment)
{
//name = "宋体";
//size = 12;
//color = Excel.Constants.xlAutomatic;
//HorizontalAlignment = Excel.Constants.xlRight;

Excel.Worksheet ws = GetSheet(wsn);
ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name;
ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;
ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;

ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;
}

public void UniteCells(Excel.Worksheet ws, int x1, int y1, int x2, int y2) //合并单元格
{
ws.get_Range(ws.Cells[x1, y1], ws.Cells[x2, y2]).Merge(Type.Missing);
}

public void UniteCells(string ws, int x1, int y1, int x2, int y2) //合并单元格
{
GetSheet(ws).get_Range(GetSheet(ws).Cells[x1, y1], GetSheet(ws).Cells[x2, y2]).Merge(Type.Missing);
}

public void InsertTable(System.Data.DataTable dt, string ws, int startX, int startY) //将内存中数据表格插入到Excel指定工作表的指定位置 为在使用模板时控制格式时使用一
{
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
for (int j = 0; j <= dt.Columns.Count - 1; j++)
{
GetSheet(ws).Cells[startX+i, j + startY] = dt.Rows[i][j].ToString();
}
}
}

public void InsertTable(System.Data.DataTable dt, Excel.Worksheet ws, int startX, int startY) //将内存中数据表格插入到Excel指定工作表的指定位置二
{
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
for (int j = 0; j <= dt.Columns.Count - 1; j++)
{
ws.Cells[startX+i, j + startY] = dt.Rows[i][j];
}
}
}

public void AddTable(System.Data.DataTable dt, string ws, int startX, int startY) //将内存中数据表格添加到Excel指定工作表的指定位置一
{
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
for (int j = 0; j <= dt.Columns.Count - 1; j++)
{
GetSheet(ws).Cells[i + startX, j + startY] = dt.Rows[i][j];
}
}
}

public void AddTable(System.Data.DataTable dt, Excel.Worksheet ws, int startX, int startY) //将内存中数据表格添加到Excel指定工作表的指定位置二
{
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
for (int j = 0; j <= dt.Columns.Count - 1; j++)
{
ws.Cells[i + startX, j + startY] = dt.Rows[i][j];
}
}
}

//public void InsertPictures(string Filename, string ws) //插入图片操作一
//{
// GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse,MsoTriState.msoTrue, 10, 10, 150, 150); //后面的数字表示位置
//}

//public void InsertPictures(string Filename, string ws, int Height, int Width) //插入图片操作二
//{
// GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
// GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;
// GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;
//}
//public void InsertPictures(string Filename, string ws, int left, int top, int Height, int Width) //插入图片操作三
//{

// GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
// GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementLeft(left);
// GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementTop(top);
// GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;
// GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;
//}

public void InsertActiveChart(Excel.XlChartType ChartType, string ws, int DataSourcesX1, int DataSourcesY1, int DataSourcesX2, int DataSourcesY2, Excel.XlRowCol ChartDataType) //插入图表操作
{
ChartDataType = Excel.XlRowCol.xlColumns;
wb.Charts.Add(Type.Missing,Type.Missing,Type.Missing,Type.Missing);
{
wb.ActiveChart.ChartType = ChartType;
wb.ActiveChart.SetSourceData(GetSheet(ws).get_Range(GetSheet(ws).Cells[DataSourcesX1, DataSourcesY1], GetSheet(ws).Cells[DataSourcesX2, DataSourcesY2]), ChartDataType);
wb.ActiveChart.Location(Excel.XlChartLocation.xlLocationAsObject, ws);
}
}

public bool Save() //保存文档
{
if (mFilename == "")
{
return false;
}
else
{
try
{
wb.Save();
return true;
}

catch //(Exception ex)
{
return false;
}
}
}

public bool SaveAs(object FileName) //文档另存为
{
try
{
//string outpathandname = sysPath + FileName;
wb.SaveAs(writePath + FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
return true;

}

catch //(Exception ex)
{
return false;

}
}

public void Close() //关闭一个Excel对象,销毁对象
{
//wb.Save();
wb.Close(Type.Missing,Type.Missing,Type.Missing);
wbs.Close();
app.Quit();
wb = null;
wbs = null;
app = null;
GC.Collect();
}

public string hostMap()
{
//string aa = InfoMS.OfficeUtility.RegularExpressionPattern.alpha;
return HostingEnvironment.MapPath(@"PrecompiledWeb\web") + " ---- abc--"; //PrecompiledWeb\web
}

}
}
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
母爱作文(围绕一句话来写)900字 月经期能喝冰糖沙梨水? 石家庄火车站骗钱 五等分的新娘手游汉化安卓游戏官方下载 昆明看守所那个看守所好在 成建军行政拘留 青岛贵捷充换电设备成套服务有限公司怎么样? 合肥贵捷商贸有限责任公司怎么样? 南京贵捷物资有限公司怎么样? 上海贵捷电气有限公司产品用途 asp.net如何读取跨行跨列的word表格 asp.net中服务器控件table怎样动态生成表格 打新股中签1个号配多手股 用asp.net怎样实现上传excel表格,然后将excel表格显示出来。 请问:股票打新股:如果中签是中1000股吗?还是这一个号码买入多少都同时中?谢谢 在ASP.NET网页中,在指定DIV中动态生成表格 asp.net怎么将表格中的内容居中? ASP.NET打印表格 腾讯手游助手渲染缓存对王者荣耀有效吗 怎样在asp.net网页中迅速建立表格? ASP.NET的表格Table如何设计? asp.net做个简单的表格 腾讯手游助手怎样显示帧数 模拟器显示帧数 怎么把腾讯手游助手的OpenGL从核显换成独显 为什么腾讯手游助手说初始化渲染引擎失败? 腾讯手游助手玩和平精英用openGL渲染,树叶纹理失真怎么设置显卡选项? 腾讯手游助手里渲染模式directy+是什么意思 怎么鉴别中华草龟和中华花龟 如何把百度的推荐关掉 中华草龟的外观特征有哪些? asp.net 动态表格显示边框怎么实现? 新股申购的中签率0.08或0.1012这是表示多少是百分之还是千分之_百度知 ... ASP.net怎么画表格 Asp.net中如何显示一下的表格,动态数据 如何用Asp.net完成在后台打开EXCEL表格 如何在 asp.net 中使用框架 ,还有表格? asp.net 展开收起表格 东莞龙眼到广州火车站怎么走? 广州摘龙眼有多少人摔死 ppt中,可以为剪贴画设置艺术效果吗? 虾放在不锈钢盆里存入冰箱一宿就变绿了.还能吃吗? 冷冻的虾没有放冰箱里,在外面虾变色了,还可以吃吗 虾放冰箱发黑能吃吗 我想清洁一下冰箱,所以想先断电,清洁一下之后再插电使用。可以吗? 电脑桌面的电脑图标消失了怎么弄,就是打开电脑里面有4个分区。 华为MateBook13蓝牙自动断开,连不上? 笔记本无法连接蓝牙耳机 脸色暗黄有斑怎么去除? 皮肤暗黄,脸上有斑很痛苦,有什么办法? 脸色很暗黄,有斑斑斑点点的,该怎么办