文件管理 · 2022年7月25日

winform导出word文档|用C#实现WORD文档导出

1. C# winform 下导出到窗体datagridview到word代码~

这个过程可耻下场参考一下: #region 使用Interop.Word.dll将DataGridView导出到Word /// <summary> /// 使用Interop.Word.dll将DataGridView导出到Word /// </summary> /// <param name="dGV"></param> /*保会通财务软件公司*/ public static void DataGridViewExportToWord_WordDll(DataGridView dGV) { Word.Document mydoc = new Word.Document();//实例化Word文档对象 Word.Table mytable;//声明Word表格 Word.Selection mysel;//声明Word选区 Object myobj; if (dGV.Rows.Count == 0) return; //建立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 = true; 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()); } } } #endregion

2. winform中怎么导出WORD文档

.net自带了一个报表控制。基本满足一般性需求。

3. 我要用C#,NET 导出一个word文档。文档内容只要一个表格。

楼上的 你能不能改写下这个例子啊? 我这个只能导出字符 不能导出表格 string title = "个人信息"; object titleLengh = title.Length; string first = "\n 公司最近需要利用C#对项目进行编程,其" + "中存在一个功能就是可自动生成WORD文档,但一直以来都" + "找不到什么好办法,无奈之下,只有自已学着写一个了."; object firstLengh = first.Length; string second = "\n 如果能真正生成WORD文档的好处有:"; object secondLengh = second.Length; string third = "\n1、根据数据库信息自动生成文档;"; object thirdLengh = third.Length; string forth = "\n2、免去书写文档之苦;"; object forthLengh = forth.Length; string fifth = "\n3、可以通过邮件方式传出文档。"; object fifthLengh = fifth.Length; object missing; object zero = 0; object c = 10; object one = 1; object two = 2; object tree = 3; object four = 4; object five = 5; object six = 6; object seven = 7; object eight = 8; object nine = 9; object ten = 10; Object Nothing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.ApplicationClass wa = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document WordDoc = wa.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); missing = System.Reflection.Missing.Value; wa.Visible = true; wa.Documents.Add(ref missing, ref missing, ref missing, ref missing); Microsoft.Office.Interop.Word.Range myRange = wa.ActiveDocument.Range(ref zero, ref zero); object r = myRange; Microsoft.Office.Interop.Word.Paragraph p = wa.ActiveDocument.Paragraphs.Add(ref r); p.Range.InsertBefore(title); //p.Range.Font.Size = 1;Microsoft.Office.Interop.Word.Range titleRange = wa.ActiveDocument.Range(ref zero, ref titleLengh); //titleRange.Font.Size = 1; titleRange.Font.Name = "幼圆"; titleRange.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlue; //MessageBox.Show("NO.1"); //titleRange.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; Microsoft.Office.Interop.Word.Range firstR = wa.ActiveDocument.Paragraphs[1].Range;//.Item(2).Range; Microsoft.Office.Interop.Word.Table table = WordDoc.Tables.Add(titleRange, 3, 3, ref Nothing, ref Nothing); r = firstR; p = wa.ActiveDocument.Paragraphs.Add(ref r);firstR.Font.Size = 40; firstR.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//在表格第一单元格中添加自定义的文字内容 table.Cell(1, 1).Range.Text = "lllll";firstR.InsertAfter(first);//firstR.InsertParagraphAfter(); //firstR=wa.ActiveDocument.Paragraphs.Item(3).Range; firstR.InsertAfter(second); firstR.InsertAfter(third); firstR.InsertAfter(forth); firstR.InsertAfter(fifth); Context.Response.Write("成功");

4. C#:怎样生成word文件

实现代码如下:public class BiultReportForm { /// <SUMMARY></SUMMARY> /// word 应用对象 /// private Microsoft.Office.Interop.Word.Application _wordApplication; /// <SUMMARY></SUMMARY> /// word 文件对象 /// private Microsoft.Office.Interop.Word.Document _wordDocument; /// <SUMMARY></SUMMARY> /// 创建文档 /// public void CreateAWord() { //实例化word应用对象 this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass(); Object myNothing = System.Reflection.Missing.Value; this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing); } /// <SUMMARY></SUMMARY> /// 添加页眉 /// /// <PARAM name="pPageHeader" /> public void SetPageHeader(string pPageHeader) { //添加页眉 this._wordApplication.ActiveWindow.View.Type =Microsoft .Office .Interop .Word.WdViewType.wdOutlineView; this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader; this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader); //设置中间对齐 this._wordApplication.Selection.ParagraphFormat.Alignment =Microsoft .Office .Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; //跳出页眉设置 this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument; } /// <SUMMARY></SUMMARY> /// 插入文字 /// /// <PARAM name="pText" />文本信息 /// <PARAM name="pFontSize" />字体打小 /// <PARAM name="pFontColor" />字体颜色 /// <PARAM name="pFontBold" />字体粗体 /// <PARAM name="ptextAlignment" />方向 public void InsertText(string pText, int pFontSize, Microsoft.Office.Interop.Word.WdColor pFontColor, int pFontBold, Microsoft.Office.Interop.Word.WdParagraphAlignment ptextAlignment) { //设置字体样式以及方向 this._wordApplication.Application.Selection.Font.Size = pFontSize; this._wordApplication.Application.Selection.Font.Bold = pFontBold; this._wordApplication.Application.Selection.Font.Color= pFontColor; this._wordApplication.Application.Selection.ParagraphFormat.Alignment = ptextAlignment; this._wordApplication.Application.Selection.TypeText(pText); } /// <SUMMARY></SUMMARY> /// 换行 /// public void NewLine() { //换行 this._wordApplication.Application.Selection.TypeParagraph(); } /// <SUMMARY></SUMMARY> /// 插入一个图片 /// /// <PARAM name="pPictureFileName" /> public void InsertPicture(string pPictureFileName) { object myNothing = System.Reflection.Missing.Value; //图片居中显示 this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing); &n

5. 跪求 c#(Form)导出word文档(从数据库取出试题),还需要对试题进行排版,要详细代码,万分感谢,求贵人

没有分,要求还多!

6. 如何C#操作word模板生成word文档并打开

codeproject上有很多操作word的C#代码: 顺便给你几个链接: http://www.codeproject.com/KB/office/Word_Automation.aspxhttp://www.codeproject.com/KB/office/Word2007Automation.aspxhttp://www.codeproject.com/KB/office/csautomateword.aspx private void met_word_automation() { try {// Declaring the object variables we will need laterobject varFileName = "c:\\temp\\doc.docx";object varFalseValue = false;object varTrueValue = true;object varMissing = Type.Missing;string varText;// Create a reference to Microsoft Word applicationMicrosoft.Office.Interop.Word.Application varWord =new Microsoft.Office.Interop.Word.Application();// Creates a reference to a Word documentMicrosoft.Office.Interop.Word.Document varDoc =varWord.Documents.Open(ref varFileName, ref varMissing,ref varFalseValue,ref varMissing, ref varMissing, ref varMissing, ref varMissing,ref varMissing, ref varMissing, ref varMissing,ref varMissing, ref varMissing, ref varMissing, ref varMissing,ref varMissing, ref varMissing);// Activate the documentvarDoc.Activate();// Change the 1st sentence of the documentvarText = "Altered sentence nr. 1";varDoc.Sentences[0].Text = varText;// Change the 3rd sentence of the documentvarText = "Altered sentence nr. 3";varDoc.Sentences[2].Text = varText;// Save the documentvarDoc.Save();// Show the Microsoft Word window with our document on itvarWord.Visible = true;// Call the print dialog in WordMicrosoft.Office.Interop.Word.Dialog varDlg =varWord.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint];varDlg.Show(ref varMissing);// Print the documentvarDoc.PrintOut(ref varTrueValue, ref varFalseValue, ref varMissing,ref varMissing, ref varMissing, ref varMissing,ref varMissing, ref varMissing, ref varMissing, ref varMissing,ref varFalseValue, ref varMissing, ref varMissing,ref varMissing, ref varMissing, ref varMissing, ref varMissing,ref varMissing);// Send mail with this document as an attachmentvarDoc.SendMail(); } catch (Exception varE) { MessageBox.Show("Error:\" + varE.Message, "Error message"); } } You need to create references to this .NET namespace in order to use the above code: Microsoft.Office.Interop.Word

7. 在winform导出到word文档上 如果电脑上没有Office(不能装) 怎么用wbs做啊 求高手指教 最好源码

用模块.用word保存一个xml文档.自己分析xml的文档格式,winform导出时就使用此格式

8. winform程序中关于sql数据库数据导出到word的问题….

/// <summary> /// 导出到Word /// </summary> /// <param name="dt">导出的数据DataTable</param> /// <param name="isColname">是否显示列名</param> public static void OutPutWordDT(DataTable dt, bool isColname) { Object Nothing = System.Reflection.Missing.Value; Word.Application oword = new Word.Application();//word Application Word.Document odoc = oword.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);//文档 odoc.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; try { //在word以表格形式存储数据 Word.Table otable = odoc.Tables.Add(oword.Selection.Range, dt.Rows.Count + 1, dt.Columns.Count, ref Nothing, ref Nothing); otable.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphThaiJustify;//设置对其方式 otable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//设置表格边框样式 if (isColname)//列名称 { int intcol = 0; for (int ii = 0; ii < dt.Columns.Count; ii++) { intcol += 1; otable.Cell(1, intcol).Range.Text = dt.Columns[ii].ColumnName; otable.Cell(1, intcol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式 } } //写表格内容 int intRow = 1; for (int ii = 0; ii < dt.Rows.Count; ii++) { intRow += 1; int intCol = 0; for (int jj = 0; jj < dt.Columns.Count; jj++) { intCol += 1; otable.Cell(intRow, intCol).Range.Text = dt.Rows[ii][jj].ToString(); otable.Cell(intRow, intCol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式 } } oword.Visible = true; } catch (Exception) { } finally { System.Diagnostics.Process[] CurrentProcess = System.Diagnostics.Process.GetProcessesByName("WINWORD"); for (int i = 0; i < CurrentProcess.Length; i++) { if (CurrentProcess[i].MainWindowHandle.ToInt32() == 0) { try { CurrentProcess[i].Kill(); } catch { } } } } }—————————————————-用代码对word操作时myWordDoc.Paragraphs.Last.Range.Text =该怎样用这个字符数组对其赋值?既然是字符数组,那就这样:假设你得到的字符数组为stringArray[ ]try{foreach(string str in stringArray){ myWordDoc.Paragraphs.Last.Range.Text =str;//赋值}}catch(Exception ex{ MessageBox.Show(ex.Message);}或者可以用FOR循环try{for(int i=0;i< stringArray.Length;i++){ myWordDoc.Paragraphs.Last.Range.Text =stringArray[i];//赋值}}catch(Exception ex{ MessageBox.Show(ex.Message);}

9. C# Winform中,怎么将RichTextBox中的字体和样式生成word文档或pdf文档

string path; Directory.CreateDirectory(@"D://hp文件夹"); path = "D://hp文件夹//" + this.textBox1.Text + ".doc"; this.richTextBox1.SaveFile(@path, RichTextBoxStreamType.RichText); 这样就可以存成word格式。编译通过

10. 用C#实现WORD文档导出

protected void btnchu1_Click(object sender, EventArgs e) { Export("application/ms-excel", "学生资料报表.xls"); } public void Export(string FileType, string FileName) { Response.ClearContent(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF7; 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); MyGridView.AllowPaging = false; MyGridView.Columns[10].Visible = false; MyGridView.Columns[0].Visible = false; bind(); MyGridView.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); MyGridView.AllowPaging = true; bind(); } public override void VerifyRenderingInServerForm(Control control) { }