C# WEB 方式下 如何在Excel里做表

SydPink 2006-04-18 12:28:56
如题,不想做报表,所以想把数据导到Excel里去,控制Excel把数据都做成表,然后在Excel里做简单排版就可打印;现在的问题是不知道如何控制Excel或者生成Excel文件。
论坛里容易找到的代码都是用 Response.Write()方法让用户下载 *.xls 文件,然后用Excel打开,但是这种方法生成的文件里并不是真正的Excel表格啊,只是简单的字符串数组样的,没有表格线,
各位XDJM有做过的提示一下!
...全文
219 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
SydPink 2006-04-18
  • 打赏
  • 举报
回复
先谢过楼上的!C#玩的不熟,WinForm上的代码怎么能变通一下用到 Web 模式下啊?我需要的是
B/S 模式上的 控制 Excel。
zwjob 2006-04-18
  • 打赏
  • 举报
回复
private void Export(System.Web.UI.WebControls.DataGrid dg,string fileName,string typeName)
{
System.Web.HttpResponse httpResponse = Page.Response;
httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8));
httpResponse.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
httpResponse.ContentType = typeName;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
dg.RenderControl(hw);
string filePath = Server.MapPath("~/downfile/")+fileName;
System.IO.StreamWriter sw = System.IO.File.CreateText(filePath);
sw.Write(tw.ToString());
sw.Close();

DownFile(httpResponse,fileName,filePath);
httpResponse.End();
}
iuhxq 2006-04-18
  • 打赏
  • 举报
回复
http://www.weste.net/html/200408/20040811QBI131735.html
http://www.pconline.com.cn/pcedu/empolder/gj/vc/10203/43433.html
http://www.programfan.com/article/showarticle.asp?id=2552
aliketen 2006-04-18
  • 打赏
  • 举报
回复
我今天就试过,以上代码不能直接使用:(
SydPink 2006-04-18
  • 打赏
  • 举报
回复
感谢 aliketen(继续学习NET!) ( ) 信誉:78 的代码。
我先试试看。
happer6012 2006-04-18
  • 打赏
  • 举报
回复
mark
aliketen 2006-04-18
  • 打赏
  • 举报
回复
用XML吧,显示效果比较好控制
aliketen 2006-04-18
  • 打赏
  • 举报
回复
private bool DownFile(System.Web.HttpResponse Response,string fileName,string fullPath)
{
try
{
Response.ContentType = "application/vnd.ms-excel";

Response.AppendHeader("Content-Disposition","attachment;filename=" +
HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) + ";charset=GB2312");
System.IO.FileStream fs= System.IO.File.OpenRead(fullPath);
long fLen=fs.Length;
int size=1024;//每1K同时下载数据
byte[] readData = new byte[size];//指定缓冲区的大小
if(size>fLen)size=Convert.ToInt32(fLen);
long fPos=0;
bool isEnd=false;
while (!isEnd)
{
if((fPos+size)>fLen)
{
size=Convert.ToInt32(fLen-fPos);
readData = new byte[size];
isEnd=true;
}
fs.Read(readData, 0, size);//读入一个压缩块
Response.BinaryWrite(readData);
fPos+=size;
}
fs.Close();
System.IO.File.Delete(fullPath);
return true;
}
catch
{
return false;
}
}
aliketen 2006-04-18
  • 打赏
  • 举报
回复
private void Export(string fileName,string typeName)
{
System.Web.HttpResponse httpResponse = Page.Response;
httpResponse.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8));
httpResponse.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
httpResponse.ContentType = application/ms-excel;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
DataSet mDSData = new DataSet();
mDSData.Tables.Add("myTable");
mDSData.Tables["myTable"].Columns.Add("ID");
mDSData.Tables["myTable"].Columns.Add("Name");
mDSData.Tables["myTable"].Columns.Add("PassWord");
for (int i = 0; i < 10; i++)
{
DataRow dr = mDSData.Tables["myTable"].NewRow();
dr["ID"] = i;
dr["Name"] = i;
dr["PassWord"] = i;
mDSData.Tables["myTable"].Rows.Add(dr);
}
tw.WriteLine("<?xml version=\"1.0\"?>");
tw.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");
tw.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
tw.WriteLine(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
tw.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
tw.WriteLine(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
tw.WriteLine(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
tw.WriteLine(" <DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">");
tw.WriteLine(" <Author>Automated Report Generator Example</Author>");
tw.WriteLine(string.Format(" <Created>{0}T{1}Z</Created>", DateTime.Now.ToString("yyyy-mm-dd"), DateTime.Now.ToString("HH:MM:SS")));
tw.WriteLine(" <Company>Your Company Here</Company>");
tw.WriteLine(" <Version>11.6408</Version>");
tw.WriteLine(" </DocumentProperties>");
tw.WriteLine(" <ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">");
tw.WriteLine(" <WindowHeight>8955</WindowHeight>");
tw.WriteLine(" <WindowWidth>11355</WindowWidth>");
tw.WriteLine(" <WindowTopX>480</WindowTopX>");
tw.WriteLine(" <WindowTopY>15</WindowTopY>");
tw.WriteLine(" <ProtectStructure>False</ProtectStructure>");
tw.WriteLine(" <ProtectWindows>False</ProtectWindows>");
tw.WriteLine(" </ExcelWorkbook>");
tw.WriteLine(" <Styles>");
tw.WriteLine(" <Style ss:ID=\"Default\" ss:Name=\"Normal\">");
tw.WriteLine(" <Alignment ss:Vertical=\"Bottom\"/>");
tw.WriteLine(" <Borders/>");
tw.WriteLine(" <Font/>");
tw.WriteLine(" <Interior/>");
tw.WriteLine(" <Protection/>");
tw.WriteLine(" </Style>");
tw.WriteLine(" <Style ss:ID=\"s21\">");
tw.WriteLine(" <Alignment ss:Vertical=\"Bottom\" ss:WrapText=\"1\"/>");
tw.WriteLine(" </Style>");
tw.WriteLine(" </Styles>");
tw.WriteLine(" <Worksheet ss:Name=\"MyReport\">");
tw.WriteLine(string.Format(" <Table ss:ExpandedColumnCount=\"{0}\" ss:ExpandedRowCount=\"{1}\" x:FullColumns=\"1\"", mDSData.Tables[0].Columns.Count.ToString(), (mDSData.Tables[0].Rows.Count+1).ToString()));
tw.WriteLine(" x:FullRows=\"1\">");
foreach (DataRow row1 in mDSData.Tables[0].Rows)
{
tw.WriteLine("<Row>");
for (int j= 0; j< mDSData.Tables[0].Columns.Count;j++)
{
tw.Write("<Cell ss:StyleID=\"s21\"><Data ss:Type=\"String\">");
tw.Write(row1[j].ToString());
tw.WriteLine("</Data></Cell>");
}
tw.WriteLine("</Row>");
}
tw.WriteLine(" </Table>");
tw.WriteLine(" <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
tw.WriteLine(" <Selected/>");
tw.WriteLine(" <Panes>");
tw.WriteLine(" <Pane>");
tw.WriteLine(" <Number>3</Number>");
tw.WriteLine(" <ActiveRow>1</ActiveRow>");
tw.WriteLine(" </Pane>");
tw.WriteLine(" </Panes>");
tw.WriteLine(" <ProtectObjects>False</ProtectObjects>");
tw.WriteLine(" <ProtectScenarios>False</ProtectScenarios>");
tw.WriteLine(" </WorksheetOptions>");
tw.WriteLine(" </Worksheet>");
tw.WriteLine(" <Worksheet ss:Name=\"Sheet2\">");
tw.WriteLine(" <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
tw.WriteLine(" <ProtectObjects>False</ProtectObjects>");
tw.WriteLine(" <ProtectScenarios>False</ProtectScenarios>");
tw.WriteLine(" </WorksheetOptions>");
tw.WriteLine(" </Worksheet>");
tw.WriteLine(" <Worksheet ss:Name=\"Sheet3\">");
tw.WriteLine(" <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
tw.WriteLine(" <ProtectObjects>False</ProtectObjects>");
tw.WriteLine(" <ProtectScenarios>False</ProtectScenarios>");
tw.WriteLine(" </WorksheetOptions>");
tw.WriteLine(" </Worksheet>");
tw.WriteLine("</Workbook>");

string filePath = Server.MapPath("")+"FileName.xls";
System.IO.StreamWriter sw = System.IO.File.CreateText(filePath);
sw.Write(tw.ToString());
sw.Close();
DownFile(httpResponse,"FileName.xls",filePath);
httpResponse.End();
}
cuiyingfeng 2006-04-18
  • 打赏
  • 举报
回复
gz
SydPink 2006-04-18
  • 打赏
  • 举报
回复
zwjob(网瘾君子) ( ) 信誉:100
代码我没有试,但是和我用过的一样,你这个是把DataGrid导入EXCEL文件,不过这个不行啊。
我不能直接导dataGrid。我把我的具体问题说一下好了:
我需要做工资单。DataGrid中有 N 条 数据,我就需要在Excel里做 N 个表,每个表都有表头和一个人的工资数据,这N个表在EXCEL里被打印后,发放到每个员工手上,让他们看到自己的工资信息后,签字用啊。目的就是这样,不知道大家理解没有。不能直接导DataGrid,DataGrid只能用来在 Web 页面上显示而已。而且DataGrid是带分页的,只有一个表头,这样也打不全。各位在帮帮忙!

62,074

社区成员

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

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

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

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