关于web上Word和Excel的处理,不晓得搞不搞得定,有经验的朋友请进来谈谈。

dawaer 2005-01-20 04:50:21
在web页面中:

(1) 想把查询查来的数据另存为excel文件,(不一定直接存web文档,可以从数据库提取数据)但关键不清楚的是如何生成excel文件;那位朋友有过类似经验的话请说一说。

(2) 想直接在web页面中打开Word文档编辑,编辑完了直接回复给发文档给我的人,也是不晓得该如何在web中把Word文件打开并保存修改;搞过的朋友给俺说说看;

如果ASP页面中不行的话,那么用ASP.net可能吗?

小弟搞web开发不久,肯望各位大侠支招;盼复。
...全文
322 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
噯卟釋手 2005-02-24
  • 打赏
  • 举报
回复
UP + 学习 + 接分

UP + 学习 + 接分

UP + 学习 + 接分
91bct 2005-02-24
  • 打赏
  • 举报
回复
我觉得孟子E章的方法挺实用的,这里贴出
一、页面部分 .aspx :
<%@ Page language="c#" Codebehind="FrmDataGridToExcel.aspx.cs" AutoEventWireup="false" Inherits="Martian.WebApp.Test.FrmDataGridToExcel" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>OutPutExcel</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
<asp:Button id="Button1" runat="server" Text="输出到Excel"></asp:Button>
</form>
</body>
</HTML>

二、后台代码部分.aspx.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Martian.WebApp.Test
{
/// <summary>
/// OutPutExcel 的摘要说明。
/// </summary>
public class FrmDataGridToExcel : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private string _ConnectionString = string.Empty;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
DataGrid1.DataSource=CreateDataSource();
DataGrid1.DataBind();
}
/// <summary>
/// 创建数据源
/// </summary>
/// <returns>DataView</returns>
ICollection CreateDataSource()
{

DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("身份证号码", typeof(string)));
dt.Columns.Add(new DataColumn("图书单价",typeof(decimal)));
dt.Columns.Add(new DataColumn("购买数量",typeof(Int32)));
dt.Columns.Add(new DataColumn("总价格",typeof(decimal)));


for (int i = 0; i < 30; i++)
{
dr = dt.NewRow();

dr[0] = "123456789123456789";
dr[1] = 100 * i /3.0;
dr[2] = i + 5;
dr[3] = (decimal)dr[1] * (Int32)dr[2];
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
/// <summary>
/// 输出到Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
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.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Cells[0].Attributes.Add("style","vnd.ms-excel.numberformat:@");
e.Item.Cells[3].Attributes.Add("style","vnd.ms-excel.numberformat:¥#,###.00");
}
}
}
}



yuanshiaa 2005-02-21
  • 打赏
  • 举报
回复
实用问题,看以后结果
lizhyifeng2 2005-02-21
  • 打赏
  • 举报
回复
up
xiaoniaofei 2005-02-21
  • 打赏
  • 举报
回复
占个位置,等下研究~
fxywkj 2005-02-21
  • 打赏
  • 举报
回复
正在研究^
lys412 2005-02-20
  • 打赏
  • 举报
回复
(1) 想把查询查来的数据另存为excel文件

goody9807() 的方法可行

(2) 想直接在web页面中打开Word文档编辑,编辑完了直接回复给发文档给我的人
public static void ExportWord(string head ,string path, DataTable dt)
{
Word.Application wordApp=null;
Word.Document wordDoc=null;
object Nothing=System.Reflection.Missing.Value;
object unit=Word.WdUnits.wdLine;
//bool preview=false;


try
{
wordApp = new Word.Application();
//确保 Word 可见
//设置以创建一个空的纯文本文档
// 将这些变量设置为 Missing.Value 可视为向
// 函数中传递空。这一点很有必要,因为引用不能
// 传递 C# 空。
object template=System.Reflection.Missing.Value;
object newTemplate=System.Reflection.Missing.Value;
object documentType=System.Reflection.Missing.Value;
object visible=true;
wordDoc = wordApp.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);


//打印文件的标题
wordApp.Selection.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter;
unit=Word.WdUnits.wdLine;
wordApp.Selection.EndKey(ref unit,ref Nothing);
wordApp.Selection.Font.Bold=1;
wordApp.Selection.Font.Size = 16;
wordApp.Selection.TypeText(head);
wordApp.Selection.TypeParagraph();
wordApp.Selection.TypeParagraph();


// 打印选择的内容


wordApp.Selection.Font.Bold=0;
wordApp.Selection.Font.Size = 10.5F;
wordApp.Selection.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphLeft;

//添加表
Word.Table table=wordDoc.Tables.Add(wordApp.Selection.Range,dt.Rows.Count+1,dt.Columns.Count,ref Nothing,ref Nothing);



//Word表中的标头,在table中显示属性
for (int i=0;i<dt.Columns.Count;i++)
table.Cell(1,i+1).Range.Text = dt.Columns[i].ToString();


for(int i=0;i<dt.Rows.Count;i++)
{
for(int j=0;j<dt.Columns.Count;j++)
{
table.Cell(i+2,j+1).Range.Text=dt.Rows[i][j].ToString();
}
}


try
{
object p =(object)(path);
wordDoc.Saved=true;
wordApp.Visible=true;
// wordDoc.ActiveWindow.View.Type=Word.WdViewType.wdPrintPreview;
// wordApp.WindowState=Word.WdWindowState.wdWindowStateMaximize;
wordApp.Activate();
//preview=true;
wordDoc.SaveAs(ref p,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);

}
catch
{
//throw new Exception("导出文件错误,请检查相关设置!");
}
}
catch(Exception p)
{
throw p;
}

/*finally
{
if(!preview)
{
wordDoc.Saved=true;
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordDoc=null;
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
wordApp=null;
}
unit=null;
Nothing=null;
GC.Collect();
}*/

}



如果ASP页面中不行的话,那么用ASP.net可能吗?
ASP.net可以

chenyingchun 2005-01-21
  • 打赏
  • 举报
回复
就是在线编辑word的一个控件.前两年写的。刚才封装了一下。还没有完全测试。希望大家多提意见。
后面还有其它的控件发布。
该控件可以用于OA,erp等系统。可以嵌入ie.

http://www.ydxxw.com/file/ltwebword.rar

有什么问题可以直接联系我:ptopinfo@163.com
qq:6782435
shooper 2005-01-20
  • 打赏
  • 举报
回复
http://www.cnblogs.com/elevenwolf/archive/2004/08/21/35324.aspx
goody9807 2005-01-20
  • 打赏
  • 举报
回复
http://blog.csdn.net/goody9807/articles/145876.aspx
goody9807 2005-01-20
  • 打赏
  • 举报
回复
你可以dataset 导入 excel参考
http://community.csdn.net/Expert/topic/3077/3077526.xml?temp=.8746912
http://www.dev-club.com/club/bbs/showEssence.asp?id=26350

http://dev.csdn.net/Develop/article/18/18623.shtm
http://community.csdn.net/Expert/topic/3112/3112296.xml?temp=.926861
http://dotnet.aspx.cc/ShowDetail.aspx?id=BF0A54F9-C7C7-4200-BD9A-802AC1F5DE50


http://expert.csdn.net/Expert/TopicView1.asp?id=2928057

www.foxhis.com/powermjtest/
原文代码:




private void Button1_Click(object sender, System.EventArgs e)
{
//写入Excel的方法:
//定义需要参数。
string SourceFile="Data.XLS"; //源文件名称。
string TemplatePath=Server.MapPath("ExcelTemplate"); //存放源文件的文件夹路径。
string DownloadPath=Server.MapPath("ExcelDownload"); //副本的文件夹路径。
//副本的文件名。
string TempFileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".XLS";
object missing = System.Reflection.Missing.Value;
Excel.Application myExcel=new Excel.Application();
//打开新文件
myExcel.Application.Workbooks.Open(TemplatePath+"\\"+SourceFile,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing, missing,missing);
Excel.Workbook myBook=myExcel.Workbooks[1];
Excel.Worksheet curSheet = (Excel.Worksheet)myBook.Sheets[2];

string DownloadFilePath=DownloadPath+"\\"+TempFileName;

int i=0;
while (i<=10)
{
myExcel.Cells[4+i,2]=i.ToString();
myExcel.Cells[4+i,3]=i.ToString();
myExcel.Cells[4+i,4]=i.ToString();
myExcel.Cells[4+i,5]=i.ToString();
myExcel.Cells[4+i,6]=i.ToString();
i++;
}

myBook.Saved=true;
//myBook.SaveAs(DownloadFilePath,missing,"","",false,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,missing,missing);

myBook.PrintPreview(0);
//myBook.PrintOut(missing,missing,missing,missing,missing,missing,missing,missing);
myBook.Close(false, null,null);
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
myBook = null;
myExcel = null;
GC.Collect();
//Response.Redirect("ExcelDownload//"+TempFileName); //下载文件
}

wangsaokui 2005-01-20
  • 打赏
  • 举报
回复
http://dotnet.aspx.cc/ShowDetail.aspx?id=6AFBF00B-459D-4642-AD14-8A4765FFAFCC
yistudio 2005-01-20
  • 打赏
  • 举报
回复
第一个问题yichuan1982的方法可以,还可以用csv文件,它是一个以“,”作为分割符的格式文件,直接用Excel打开的
private void DataSetToExcel()
{
System.Data.SqlClient.SqlConnection objCn=new System.Data.SqlClient.SqlConnection();
objCn.ConnectionString=Application["connection"].ToString();

try
{
System.Text.StringBuilder objSb=new System.Text.StringBuilder();
System.IO.MemoryStream objMs=new System.IO.MemoryStream();
System.IO.StreamWriter objSw=new System.IO.StreamWriter(objMs,System.Text.Encoding.Default);

string strSQL=null;
if(Page.Request["condition"]=="")
strSQL="select SoftName,SoftSortName,SystemNumber,BuyNumber,SoftPrice,BuyDate,EnterDate,StockNumber,Remark from V_SoftManage";
else
strSQL="select SoftName,SoftType from T_Soft where "+Page.Request["condition"].Replace("@","%");
DataSet objDs=Hosii.Data.SqlHelper.ExecuteDataset(objCn,CommandType.Text,strSQL);
objSb.Append("软件名称,软件种类");
objSw.WriteLine(objSb.ToString());
for(int i=0;i<objDs.Tables[0].Rows.Count;i++)
{
objSb.Remove(0,objSb.Length);
for(int j=0;j<objDs.Tables[0].Columns.Count;j++)
{
if(j==7)
{
if(objDs.Tables[0].Rows[i][j].ToString()=="0")
objSb.Append("无,");
else
objSb.Append("有,");
}
else
objSb.Append(objDs.Tables[0].Rows[i][j].ToString()+",");
}
objSw.WriteLine(objSb.ToString());
}
objSw.Flush();
byte[] b=objMs.ToArray();
objSw.Close();
Response.ContentType="application/octet-stream";
Response.AddHeader("Content-disposition", "attachment;filename=SoftManageOut.csv");
Response.OutputStream.Write(b, 0, b.Length);
Response.Flush();
Response.End();
}
catch(Exception ex)
{
this.WriteSystemErrorLogo("DataSetToExcel",ex.Message);
}
finally
{
if(objCn.State==ConnectionState.Open) objCn.Close();
}
}
第二个问题不好解决,得自己写一个OCX控件来实现
andrawsky 2005-01-20
  • 打赏
  • 举报
回复
到处都有DATAGRID生成EXCEL的例子,WROD也有

Response.Clear();
Response.Buffer= true;
Response.Charset="utf-8";
Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode("企业资质统计.xls"));
Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文
// Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
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.dgrd_qiye.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();


点BUTTON以后就自动生成,弹出对话框让用户保存
kaixin110 2005-01-20
  • 打赏
  • 举报
回复
朋友请具体点好吗
yichuan1982 2005-01-20
  • 打赏
  • 举报
回复
到处都有DATAGRID生成EXCEL的例子,WROD也有

Response.Clear();
Response.Buffer= true;
Response.Charset="utf-8";
Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode("企业资质统计.xls"));
Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文
// Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
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.dgrd_qiye.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();


点BUTTON以后就自动生成,弹出对话框让用户保存

62,041

社区成员

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

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

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

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