贴出asp.net输出Excel并添加标题的代码,仅供参考!

loulanlouzhu 2004-03-14 10:54:59
主要功能:1。添加标题
2。设置输出格式
3。解决服务器打开多个Excel进程的问题


public void OutputExcel(DataView dv,string str)
{
//
// TODO: 在此处添加构造函数逻辑
//
//dv为要输出到Excel的数据,str为标题名称
GC.Collect();
Application excel;// = new Application();
int rowIndex=4;
int colIndex=1;

_Workbook xBk;
_Worksheet xSt;

excel= new ApplicationClass();

xBk = excel.Workbooks.Add(true);

xSt = (_Worksheet)xBk.ActiveSheet;

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

//
//取得表格中的数据
//
foreach(DataRowView row in dv)
{
rowIndex ++;
colIndex = 1;
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();
}
}
}
//
//加载一个合计行
//
int rowSum = rowIndex + 1;
int colSum = 2;
excel.Cells[rowSum,2] = "合计";
xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
//
//设置选中的部分的颜色
//
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[2,2] = str;
//
//设置整个报表的标题格式
//
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;
//
//设置报表表格为最适应宽度
//
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();
//
//设置整个报表的标题为跨列居中
//
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
//
//绘制边框
//
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗
xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置上边线加粗
xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗
xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗
//
//显示效果
//
excel.Visible=true;

//xSt.Export(Server.MapPath(".")+"\\"+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML);
xBk.SaveCopyAs(Server.MapPath(".")+"\\"+this.xlfile.Text+".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(this.xlfile.Text+".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";

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

Response.End();
}
...全文
203 56 打赏 收藏 转发到动态 举报
写回复
用AI写文章
56 条回复
切换为时间正序
请发表友善的回复…
发表回复
Happyboy_zjy 2004-03-26
  • 打赏
  • 举报
回复
TO:godliu521(勿以分多而顶之;勿以分少而不顶)
这个是指ASPNet用户没有写你所指定的文件的权限,把某个文件的权限改为允许ASPNET用户写的权限即可
sunyo 2004-03-17
  • 打赏
  • 举报
回复
GOOD
godliu521 2004-03-17
  • 打赏
  • 举报
回复
System.Runtime.InteropServices.COMException: HRESULT 中的异常:0x800A03EC。

源错误:


行 225: xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;
行 226: //xQt = xSt.QueryTables.Add(Conn,xSt.get_Range(excel.Cells[4,2],excel.Cells[4,2]),Select);
行 227: xQt = xSt.QueryTables.Add(Conn,xSt.get_Range(excel.Cells[0,0],excel.Cells[0,0]),Select);


pangzi2627 2004-03-17
  • 打赏
  • 举报
回复
不错,不错!
楼主真是好人,希望大家能够多贴出这样的好文章。
先谢谢了!收藏
godliu521 2004-03-17
  • 打赏
  • 举报
回复
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.UnauthorizedAccessException: 拒绝访问。

ASP.NET 未被授权访问所请求的资源。请考虑授予 ASP.NET 请求标识访问此资源的权限。ASP.NET 有一个在应用程序没有模拟时使用的基进程标识(通常,在 IIS 5 上为 {MACHINE}\ASPNET,在 IIS 6 上为网络服务)。如果应用程序正在通过 <identity impersonate="true"/> 模拟,则标识将为匿名用户(通常为 IUSR_MACHINENAME)或经过身份验证的请求用户。

若要授予 ASP.NET 对文件的写访问权,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的用户或组。突出显示 ASP.NET 帐户,选中所需访问权限对应的框。

源错误:


行 80: _Worksheet xSt;
行 81:
行 82: excel= new ApplicationClass();
行 83:
行 84: xBk = excel.Workbooks.Add(true);



这个怎么解决啊,大哥门啊
linyoung 2004-03-17
  • 打赏
  • 举报
回复
up
kuibobo 2004-03-17
  • 打赏
  • 举报
回复
xxi
sandynet 2004-03-17
  • 打赏
  • 举报
回复
GOOD!~
随便问个问题啊,输出的EXCEL要冻结某些单元格,代码怎么写呢?
谢谢拉:)
lufly2000 2004-03-17
  • 打赏
  • 举报
回复
GZ
wacle 2004-03-16
  • 打赏
  • 举报
回复
支持!
bing314 2004-03-16
  • 打赏
  • 举报
回复
将帖子提前
Lostinet 2004-03-16
  • 打赏
  • 举报
回复
支持!
tzhguan 2004-03-16
  • 打赏
  • 举报
回复
mark
Jouzen 2004-03-16
  • 打赏
  • 举报
回复
另外希望能加上分页显示和Excel模版的调用,这样来基本解决打印问题。
Jouzen 2004-03-16
  • 打赏
  • 举报
回复
再问以下owc10的代码差别大吗?谁能贴出来?

俺也想看看楼上说的两本书jouzen@163.net
comy 2004-03-16
  • 打赏
  • 举报
回复
谁有<<Microsoft Office Web组件和服务器扩展参考手册>>和<<Microsort Office 2000Web Components编程技术内幕>>电子版
感激不尽!
bestcomy@hotmail.com
xdt2003 2004-03-16
  • 打赏
  • 举报
回复
研究研究
loulanlouzhu 2004-03-15
  • 打赏
  • 举报
回复
不错 如果我用owc10 能完成以上功能吗?

那样就不用安装office了 只要安装owc10就可以了
--》

owc10没试过!你可以试试!
greystar 2004-03-15
  • 打赏
  • 举报
回复
gz
newnan 2004-03-15
  • 打赏
  • 举报
回复
强劲!澎湃!
收藏!
神出鬼没!!
加载更多回复(36)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧