C#网页页面内容生成excel怎么弄?

deepmist 2009-12-15 10:30:53
output.aspx页面有一个表单,里面有文本框用于输入需要查询的编码,
输入后提交表单,页面就会将输入的编码提交到CreateDataTableServlet.aspx并返回output.aspx页面生成一张带公司logo的表格,
现在需要完成的是对output.aspx页面添加一个输出excel的按钮,
对这张表格进行excel输出,
网上找了很多方法 看的有点头大
请大家说说怎么实现

刚看了下网页的源代码,居然没有表格的html代码,怎么回事啊
...全文
348 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
deepmist 2009-12-15
  • 打赏
  • 举报
回复
顶下
deepmist 2009-12-15
  • 打赏
  • 举报
回复
网上看的头大
高手们点拨下
平生我自如 2009-12-15
  • 打赏
  • 举报
回复
那就是导处到EXCEL中了 !你搜搜!网上 这些资料很多的阿
deepmist 2009-12-15
  • 打赏
  • 举报
回复
高手帮帮忙啊
woshifou 2009-12-15
  • 打赏
  • 举报
回复
学习。
昙花一现1 2009-12-15
  • 打赏
  • 举报
回复
若服务器上装有Excel,则11楼的也可以!
jingshuaizhjason 2009-12-15
  • 打赏
  • 举报
回复
C# datatable保存为excel文件的方法

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;



namespace Personal

{

public class Print

{

/// <summary>

/// 将DataTabl内容打印出来

/// Table 表, strHead 表名,Zoom 缩放比%

/// </summary>

private void baobiao(DataTable Table, string strHead, sbyte Zoom)

{

try

{



Excel.Application excelKccx = new Excel.Application(); //创建excel对象

Excel._Workbook xBk;

Excel._Worksheet xSt;

xBk = excelKccx.Workbooks.Add(true);

xSt = (Excel._Worksheet)xBk.ActiveSheet;

Excel.Range range = xSt.get_Range(excelKccx.Cells[1, 1], excelKccx.Cells[1, 22]);

range.ClearContents();//先把Range内容清除,合并才不会出错

range.MergeCells = true;

range.Value2 = strHead;//"普通医保住院定额超支管理报表(一般住院)";

range.Font.Bold = true;

range.Font.Size = 15;

range.Font.ColorIndex = 4;//颜色

range.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter; //设置标题格式为居中对齐

// 设置表头

int row = 2;

for (int i = 0; i < Table.Columns.Count; i++)//取字段名

{

excelKccx.Cells[row, i + 1] = Table.Columns[i].ColumnName.ToString();

xSt.get_Range(excelKccx.Cells[row, i + 1], excelKccx.Cells[row, i + 1]).Interior.ColorIndex = 5;

}



xSt.get_Range(excelKccx.Cells[row, 1], excelKccx.Cells[row, Table.Columns.Count]).EntireColumn.AutoFit();//自动调整列宽

xSt.get_Range(excelKccx.Cells[row, 1], excelKccx.Cells[row, Table.Columns.Count]).Font.Bold = true;



for (int i = 0; i < Table.Rows.Count; i++)//取记录值

{

row++;

for (int j = 0; j < Table.Columns.Count; j++)

{

excelKccx.Cells[row, j + 1] = Table.Rows[i][j].ToString();

}

}





excelKccx.Visible = true;

object oMissing = System.Reflection.Missing.Value;

xSt.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA3;//设置纸张大小

xSt.PageSetup.Zoom = Zoom; //缩放比例

xSt.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;//横向打印 xlPortrait1-纵向,2-横向;

xSt.PrintOut(oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);



xBk.PrintOut(oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); // 打印:

// xBk.PrintPreview(oMissing);//打印预览

xBk.Close(null, null, null);



excelKccx.Workbooks.Close();

excelKccx.Application.Quit();

excelKccx.Quit();



System.Runtime.InteropServices.Marshal.ReleaseComObject(excelKccx);

GC.Collect();//强行销毁

}

catch

{

GC.Collect();//强行销毁

};



}

}

}


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Export(GetData(), @"D:\aa");
}
}
public System.Data.DataTable GetData()
{
System.Data.DataTable dt = new System.Data.DataTable();
DataColumn dc1 = new DataColumn("Name");
DataColumn dc2 = new DataColumn("Age");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);

DataRow dr1 = dt.NewRow();
dr1[0] = "Lily";
dr1[1] = "10";
DataRow dr2 = dt.NewRow();
dr2[0] = "Lucy";
dr2[1] = "9";
dt.Rows.Add(dr1);
dt.Rows.Add(dr2);
return dt;
}

public static void Export(System.Data.DataTable dt, string filePath)
{
if (dt == null)
{
throw new Exception("数据表中无数据");
}
int eRowIndex = 1;
int eColIndex = 1;
int cols = dt.Columns.Count;
int rows = dt.Rows.Count;
Excel.Application xlApp = new Excel.ApplicationClass();
Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
try
{
//列名的处理
for (int i = 0; i < cols; i++)
{
xlApp.Cells[eRowIndex, eColIndex] = dt.Columns[i].ColumnName;
eColIndex++;
}
//列名加粗显示
xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[eRowIndex, cols]).Font.Bold = true;
xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[rows + 1, cols]).Font.Name = "Arial";
xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[rows + 1, cols]).Font.Size = "10";
eRowIndex++;

for (int i = 0; i < rows; i++)
{
eColIndex = 1;
for (int j = 0; j < cols; j++)
{
xlApp.Cells[eRowIndex, eColIndex] = dt.Rows[i][j].ToString();
eColIndex++;
}
eRowIndex++;
}
//控制单元格中的内容。
xlApp.Cells.EntireColumn.AutoFit();

xlApp.DisplayAlerts = false;
xlBook.SaveCopyAs(filePath);
xlApp.Workbooks.Close();

}
catch
{
throw;
}
finally
{
xlApp.Quit();
//杀掉Excel进程。
GC.Collect();
}
}
}
//记得要添加Com+
using Microsoft.Office;


昙花一现1 2009-12-15
  • 打赏
  • 举报
回复
如下,手动通过后台代码生成所要显示的Table,并将其显示到页面上的某个Label中即可,然后通过btnToExcel_Click进行生成EXCEL


string strInfo = "<table style=\"border:#000000 1px solid;border-collapse:collapse;\" cellSpacing=\"0\" cellPadding=\"0\" align=\"center\" width=\"100%\"><tr><td align=\"center\" style=\"padding:2px 0px 2px 0px;FONT-WEIGHT: bold; FONT-SIZE: 14px\">里程碑描述</td><td align=\"center\" style=\"padding:2px 0px 2px 0px;BORDER-left: rgb(0,0,0) 1px solid; FONT-WEIGHT: bold; FONT-SIZE: 14px\">机组</td><td align=\"center\" style=\"padding:2px 0px 2px 0px;BORDER-left: rgb(0,0,0) 1px solid; FONT-WEIGHT: bold; FONT-SIZE: 14px\">时间</td><td align=\"center\" style=\"padding:2px 0px 2px 0px;BORDER-left: rgb(0,0,0) 1px solid; FONT-WEIGHT: bold; FONT-SIZE: 14px\">完成及预测</td><td align=\"center\" style=\"padding:2px 0px 2px 0px;BORDER-left: rgb(0,0,0) 1px solid; FONT-WEIGHT: bold; FONT-SIZE: 14px\">备注</td></tr>";
//遗留
if(dtMorra.Rows.Count > 0)
{
strInfo += "<tr><td style=\"padding:2px 0px 2px 0px;BORDER-top: rgb(0,0,0) 1px solid;FONT-WEIGHT: bold; FONT-SIZE: 14px\" colSpan=\"5\">遗留</td></tr>";
for(int i = 0;i<dtMorra.Rows.Count;i++)
{
strInfo += "<tr><td style=\"padding:2px 0px 2px 0px;BORDER-top: rgb(0,0,0) 1px solid;\" width=\"45%\">"+dtMorra.Rows[i]["MS_NM"].ToString()+"</td><td style=\"padding:2px 0px 2px 0px;BORDER-top: rgb(0,0,0) 1px solid;BORDER-left: rgb(0,0,0) 1px solid;\" align=\"center\" width=\"10%\">"+dtMorra.Rows[i]["UNIT"].ToString()+"</td><td style=\"padding:2px 0px 2px 0px;BORDER-top: rgb(0,0,0) 1px solid;BORDER-left: rgb(0,0,0) 1px solid;\" align=\"center\" width=\"10%\">"+dtMorra.Rows[i]["PLAN_DATE"].ToString()+"</td><td style=\"padding:2px 0px 2px 0px;BORDER-top: rgb(0,0,0) 1px solid;BORDER-left: rgb(0,0,0) 1px solid;\" width=\"15%\">"+dtMorra.Rows[i]["FINISH_STATE"].ToString()+"</td><td style=\"padding:2px 0px 2px 0px;BORDER-top: rgb(0,0,0) 1px solid;BORDER-left: rgb(0,0,0) 1px solid;\" width=\"20%\">"+dtMorra.Rows[i]["TRACK_MEMO"].ToString()+"</td></tr>";
}
}
strInfo += "</table>";

this.lblDisplay.Text = strInfo;

/// <summary>




/// 导出数据到EXCEL
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
#region private void btnToExcel_Click(object sender, System.EventArgs e)
private void btnToExcel_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312"; //设置了类型为中文防止乱码的出现
string strFileName = DateTime.Now.ToString("yyyy-MM") + HttpUtility.UrlEncode("[一级]遗留、本月完成及未来预测",System.Text.Encoding.UTF8);
Response.AppendHeader("Content-Disposition","attachment;filename=" + strFileName + ".xls"); //定义输出文件和文件名
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为word文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.lblDisplay.RenderControl(oHtmlTextWriter);
string charset ="<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">";//防止导出后的Word文件内容乱码
Response.Write(charset+oStringWriter.ToString());
Response.End();
}
#endregion


abaochan 2009-12-15
  • 打赏
  • 举报
回复
那就是导处到EXCEL中了 !你搜搜!网上 这些资料很多的阿
Hamsic 2009-12-15
  • 打赏
  • 举报
回复
VB
                  Me.Response.ClearContent()   
Me.Response.ClearHeaders()
Me.Response.ContentType = "application/vnd.ms-excel"
Me.Response.AddHeader("Content-Disposition", "inline;filename='Test.xls'")
Me.Response.WriteFile("文件所在路径")
Me.Response.End()


给你参考
ztenv 2009-12-15
  • 打赏
  • 举报
回复
没有搞过web。。。。。。
deepmist 2009-12-15
  • 打赏
  • 举报
回复
没人回答???

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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