asp.net生成word报表web

编程入门 行业动态 更新时间:2024-10-28 06:21:39
1.使用aspnet将word表单导入系统中形成web表单,在系统中完成web

web表单要有个东西来接收用GridDataSetOpenFileDialog openFile = new OpenFileDialog();openFile.Filter = "Excel files(*.xls)|*.xls";ExcelIO excelio = new ExcelIO();if(openFile.ShowDialog()==DialogResult.OK){ if(excelio!=null) excelio.Close(); excelio = new ExcelIO(openFile.FileName); object[,] range = excelio.GetRange(); excelio.Close(); DataSet ds = new DataSet("xlsRange"); int x = range.GetLength(0); int y = range.GetLength(1); DataTable dt = new DataTable("xlsTable"); DataRow dr; DataColumn dc; ds.Tables.Add(dt); for(int c=1; c

2.ASP.NET(C#):如何利用word模板dot文件生成报表

ASP.NET利用dot模板生成Word文档代码,假定有一个模板文件位于网站Mywebsite/templates/a.dot,现在需要利用Word.ApplicationClass和Word.Document类来生成一个word文档。

关键代码如下: //初始化生成word文档需要的信息 Word.Application appWord =null; Word.Document doc = null; try { appWord = new Word.ApplicationClass(); appWord.Visible = false; object objTrue = true; object objFalse = false; object objTemplate = Server.MapPath(@"templates\a.dot");//模板路径 object objDocType = Word.WdDocumentType.wdTypeDocument; doc = appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue); //第一步生成word文档 //定义书签变量 object obDD_Name = "DD_Name";//姓 名 object obDD_Sex = "DD_Sex";//性 别 object obDD_Age = "DD_Age";//年龄 object obDD_Birthday = "DD_Birthday"; //出生年月 object obDD_Nation = "DD_Nation"; //民 族 object obDD_Native = "DD_Native"; //籍 贯 //第二步 读取数据,填充数据集 SqlDataReader dr = XXXXX;//读取出来的数据集 //第三步 给书签赋值 //给书签赋值 doc.Bookmarks.get_Item(ref obDD_Name).Range.Text = dr["appliername"].ToString(); //姓 名 doc.Bookmarks.get_Item(ref obDD_Sex).Range.Text = getCodeFullNameStd("性别", dr["Sex"].ToString());//性 别 doc.Bookmarks.get_Item(ref obDD_Age).Range.Text = Convert.ToString(DateTime.Now.Year - ((DateTime)dr["Birthday"]).Year);//年龄 . //第四步 生成word object filename = Server.MapPath("file") + "\\" + "XXXXXXXXXX情况表" +".doc"; object miss = System.Reflection.Missing.Value; doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss); object missingValue = Type.Missing; object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges; doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue); appWord.Application.Quit(ref miss, ref miss, ref miss); doc = null; appWord = null; } catch (System.Exception e) { //捕捉异常,如果出现异常则清空实例,退出word,同时释放资源 string aa = e.ToString(); object miss = System.Reflection.Missing.Value; object missingValue = Type.Missing; object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges; doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue); appWord.Application.Quit(ref miss, ref miss, ref miss); doc = null; appWord = null; }。

3.asp 制作Excel报表,并应用

// --- 源码如下,有说明。

using System;using System.Web.Hosting;using Excel = Microsoft.Office.Interop.Excel;namespace InfoMS.OfficeUtility{ public class Excel2003 { public string readPath; public string writePath; public string mFilename; public Excel.Application app; public Excel.Workbooks wbs; public Excel.Workbook wb; public Excel.Worksheets wss; public Excel.Worksheet ws; public Excel2003(string readpath, string writepath) { this.readPath = readpath; this.writePath = writepath; } public void Create()//创建一个Excel对象 { app = new Excel.Application(); wbs = app.Workbooks; wb = wbs.Add(true); } public void Open(string FileName)//打开一个Excel文件 { app = new Excel.Application(); wbs = app.Workbooks; wb = wbs.Add(readPath + FileName); //wb = wbs.Open(FileName, 0, true, 5,"", "", true, Excel.XlPlatform.xlWindows, "t", false, false, 0, true,Type.Missing,Type.Missing); //wb = wbs.Open(FileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Excel.XlPlatform.xlWindows,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing); mFilename = FileName; } public Excel.Worksheet GetSheet(string SheetName) //获取一个工作表 { Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets[SheetName]; return s; } public Excel.Worksheet AddSheet(string SheetName) //添加一个工作表 { Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets.Add(Type.Missing,Type.Missing,Type.Missing,Type.Missing); s.Name = SheetName; return s; } public void DelSheet(string SheetName)//删除一个工作表 { ((Excel.Worksheet)wb.Worksheets[SheetName]).Delete(); } public Excel.Worksheet ReNameSheet(string OldSheetName, string NewSheetName)//重命名一个工作表一 { Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets[OldSheetName]; s.Name = NewSheetName; return s; } public Excel.Worksheet ReNameSheet(Excel.Worksheet Sheet, string NewSheetName)//重命名一个工作表二 { Sheet.Name = NewSheetName; return Sheet; } public void SetCellValue(Excel.Worksheet ws, int x, int y, object value) //ws:要设值的工作表 X行Y列 value 值 { ws.Cells[x, y] = value; } public void SetCellValue(string ws, int x, int y, object value) //ws:要设值的工作表的名称 X行Y列 value 值 { GetSheet(ws).Cells[x, y] = value; } //设置一个单元格的属性 字体, 大小,颜色 ,对齐方式 public void SetCellProperty(Excel.Worksheet ws, int Startx, int Starty, int Endx, int Endy, int size, string name, Excel.Constants color, Excel.Constants HorizontalAlignment) { name = "宋体"; size = 12; color = Excel.Constants.xlAutomatic; HorizontalAlignment = Excel.Constants.xlRight; ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name; ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size; ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color; ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment; } public void SetCellProperty(string wsn, int Startx, int Starty, int Endx, int Endy, int size, string name, Excel.Constants color, Excel.Constants HorizontalAlignment) { //name = "宋体"; //size = 12; //color = Excel.Constants.xlAutomatic; //HorizontalAlignment = Excel.Constants.xlRight; Excel.Worksheet ws = GetSheet(wsn); ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name; ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size; ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color; ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment; } public void UniteCells(Excel.Worksheet ws, int x1, int y1, int x2, int y2) //合并单元格 { ws.get_Range(ws.Cells[x1, y1], ws.Cells[x2, y2]).Merge(Type.Missing); } public void UniteCells(string ws, int x1, int y1, int x2, int y2) //合并单元格 { GetSheet(ws).get_Range(GetSheet(ws).Cells[x1, y1], GetSheet(ws).Cells[x2, y2]).Merge(Type.Missing); } public void InsertTable(System.Data.DataTable dt, string ws, int startX, int startY) //将内存中数据表格插入到Excel指定工作表的指定位置 为在使用模板时控制格式时使用一 { for (int i = 0; i

4.请教生成word文档的方法

注意引用using Microsoft.Office.Interop.Word;创建代码如下:Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.Application();//初始化wordApp组件Document myWordDoc;//创建文档对象Object Nothing = System.Reflection.Missing.Value;myWordDoc = myWordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);//中间可以向word中插入文字、图片、表格等内容myWordDoc.SaveAs2(docPath, null, ref Nothing);//保存word文档//关闭WordDoc文档对象 myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);//关闭WordApp组件对象 myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);。

5. 页面导出为word

npoi可以导出为docx格式的word文档,具体怎么用表格,没有试过,给你一段导出的代码,你可以参考

using (DocX document = DocX.Create(Server.MapPath("~/Temp/ScoreResultDoc") + "/ScoreResultDoc.docx"))

{

Paragraph p = document.InsertParagraph();

// Append some text and add formatting.

p.Append("河北省信用档案\n")

.Font(new FontFamily("Times New Roman"))

.FontSize(24)

.Color(Color.Black)

.Bold();

p.Alignment = Alignment.center;

//p.Direction = Direction;

p.Append("记分告知单\n")

.Font(new FontFamily("Times New Roman"))

.FontSize(24)

.Color(Color.Black)

.Bold();

p.Alignment = Alignment.center;

Paragraph p1 = document.InsertParagraph();

//p1.Append(" ")

// .Font(new FontFamily("Times New Roman"))

// .FontSize(14)

// .Color(Color.Black)

// .Bold();

//float ff = 14.0f * 1.0f / 2.83f / 10.0f;

p1.IndentationFirstLine = f14;

p1.Append(businessName).UnderlineStyle(UnderlineStyle.singleLine)

.Font(new FontFamily("宋体"))

.FontSize(14)

.Color(Color.Black)

.Bold();

p1.Alignment = Alignment.left;

p1.Append(": 编号:[ ]号")

.Font(new FontFamily("宋体"))

.FontSize(14);

p1.Alignment = Alignment.left;

p1.SetLineSpacing(LineSpacingType.Line, 1.5f);

document.Save();

}

6.我要用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("成功");。

转载请注明出处51数据库 » asp生成word报表web

  • 0
  • 0
  • 0
  • 0
  • 0

更多推荐

asp.net生成word报表web

本文发布于:2023-06-04 22:11:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/506096.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:报表   net   asp   web   word

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!