希望大家多指点!!
public bool ExportDGV(DataGridView dgv, bool isShowWord)
{
Word.Document mydoc = new Word.Document();//实例化Word文档对象
Word.Table mytable;//声明Word表格
Word.Selection mysel;//声明Word选区
Object myobj;
if (dgv.Rows.Count == 0)
return false;
//建立Word对象
Word.Application word = new Word.Application();
myobj = System.Reflection.Missing.Value;
mydoc = word.Documents.Add(ref myobj, ref myobj, ref myobj, ref myobj);
word.Visible = isShowWord;
mydoc.Select();
mysel = word.Selection;
//将数据生成Word表格文件
mytable = mydoc.Tables.Add(mysel.Range, dgv.RowCount, dgv.ColumnCount, ref myobj, ref myobj);
//设置列宽
mytable.Columns.SetWidth(80, Word.WdRulerStyle.wdAdjustNone);
//输出列标题数据
for (int i = 0; i < dgv.ColumnCount; i++)
{
mytable.Cell(1, i + 1).Range.InsertAfter(dgv.Columns[i].HeaderText);
}
//输出控件中的记录
for (int i = 0; i < dgv.RowCount - 1; i++)
{
for (int j = 0; j < dgv.ColumnCount; j++)
{
mytable.Cell(i + 2, j + 1).Range.InsertAfter(dgv[j, i].Value.ToString());
}
}
return true;
}
public bool ExportDGV(DataGridView dgv, bool isShowWord)
{
Word.Document mydoc = new Word.Document();//实例化Word文档对象
Word.Table mytable;//声明Word表格
Word.Selection mysel;//声明Word选区
Object myobj;
if (dgv.Rows.Count == 0)
return false;
//建立Word对象
Word.Application word = new Word.Application();
myobj = System.Reflection.Missing.Value;
mydoc = word.Documents.Add(ref myobj, ref myobj, ref myobj, ref myobj);
word.Visible = isShowWord;
mydoc.Select();
mysel = word.Selection;
//将数据生成Word表格文件
mytable = mydoc.Tables.Add(mysel.Range, dgv.RowCount, dgv.ColumnCount, ref myobj, ref myobj);
//设置列宽
mytable.Columns.SetWidth(80, Word.WdRulerStyle.wdAdjustNone);
//输出列标题数据
for (int i = 0; i < dgv.ColumnCount; i++)
{
mytable.Cell(1, i + 1).Range.InsertAfter(dgv.Columns[i].HeaderText);
}
//输出控件中的记录
for (int i = 0; i < dgv.RowCount - 1; i++)
{
for (int j = 0; j < dgv.ColumnCount; j++)
{
mytable.Cell(i + 2, j + 1).Range.InsertAfter(dgv[j, i].Value.ToString());
}
}
return true;
}
也可
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Reflection; namespace OpenDoc
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1; private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
} /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
//Make sure that Word will be visible.
app.Visible=true;
// Setting these variables to Missing.Value is comparable
// to not providing a value for an optional parameter in VB.
// This gets the default behavior.
string path=Page.Request.MapPath( " ");
object fileName = path + "/example3.doc ";
object optional = Missing.Value; Microsoft.Office.Interop.Word._Document doc = app.Documents.Open2000( ref fileName,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional);
object first=0;
object last=doc.Characters.Count;
doc.Range(ref first, ref last).Select(); doc.Range(ref first, ref last).Cut(); fileName += "_new "; doc.SaveAs2000( ref fileName,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional); object saveChanges = true;
app.Quit(ref saveChanges, ref optional, ref optional); }
}
}