当前位置:编程学习 > C#/ASP.NET >>

一个带图片的考题,怎么写到WORD里面,谢谢

最近搞出题系统,遇到上面问题,咋搞呢,没思路啊 --------------------编程问答-------------------- 读取到word 里面去呀
 或者直接复制了 呵呵 --------------------编程问答-------------------- 不行吧,是保存在本地的文件 --------------------编程问答-------------------- :用C#实现动态生成Word文档,在Word文档中插入表格,并将读出的数据填入到表格中。 

要使用C#操作word,首先要添加引用: 

1、添加引用->COM->Microsoft Word 11.0 Object Library 

2、在.cs文件中添加 

using Word; 
下面的例子中包括C#对Word文档的创建、插入表格、设置样式等操作: 

(例子中代码有些涉及数据信息部分被省略,重要是介绍一些C#操作word文档的方法) 

public string CreateWordFile(string CheckedInfo) 
...{ 
string message = ""; 
try 
...{ 
Object Nothing = System.Reflection.Missing.Value; 
Directory.CreateDirectory("C:/CNSI"); //创建文件所在目录 
string name = "CNSI_" + DateTime.Now.ToShortString()+".doc"; 
object filename = "C://CNSI//" + name; //文件保存路径 
//创建Word文档 
Word.Application WordApp = new Word.ApplicationClass(); 
Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); 

//添加页眉 
WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; 
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; 
WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]"); 
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐 
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置 

WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距 

//移动焦点并换行 
object count = 14; 
object WdLine = Word.WdUnits.wdLine;//换一行; 
WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点 
WordApp.Selection.TypeParagraph();//插入段落 

//文档中创建表格 
Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing); 
//设置表格样式 
newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap; 
newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; 
newTable.Columns[1].Width = 100f; 
newTable.Columns[2].Width = 220f; 
newTable.Columns[3].Width = 105f; 

//填充表格内容 
newTable.Cell(1, 1).Range.Text = "产品详细信息表"; 
newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体 
//合并单元格 
newTable.Cell(1, 1).Merge(newTable.Cell(1, 3)); 
WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中 
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中 

//填充表格内容 
newTable.Cell(2, 1).Range.Text = "产品基本信息"; 
newTable.Cell(2, 1).Range.Font.Color = Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色 
//合并单元格 
newTable.Cell(2, 1).Merge(newTable.Cell(2, 3)); 
WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; 

//填充表格内容 
newTable.Cell(3, 1).Range.Text = "品牌名称:"; 
newTable.Cell(3, 2).Range.Text = BrandName; 
//纵向合并单元格 
newTable.Cell(3, 3).Select();//选中一行 
object moveUnit = Word.WdUnits.wdLine; 
object moveCount = 5; 
object moveExtend = Word.WdMovementType.wdExtend; 
WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend); 
WordApp.Selection.Cells.Merge(); 
//插入图片 
string FileName = Picture;//图片所在路径 
object LinkToFile = false; 
object SaveWithDocument = true; 
object Anchor = WordDoc.Application.Selection.Range; 
WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor); 
WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度 
WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高度 
//将图片设置为四周环绕型 
Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape(); 
s.WrapFormat.Type = Word.WdWrapType.wdWrapSquare; 

newTable.Cell(12, 1).Range.Text = "产品特殊属性"; 
newTable.Cell(12, 1).Merge(newTable.Cell(12, 3)); 
//在表格中增加行 
WordDoc.Content.Tables[1].Rows.Add(ref Nothing); 

WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款” 
WordDoc.Paragraphs.Last.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; 

//文件保存 
WordDoc.SaveAs(ref filename, 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); 
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing); 
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing); 
message=name+"文档生成成功,以保存到C:\CNSI\下"; 

catch 
...{ 
message = "文件导出异常!"; 

return message; 

--------------------编程问答-------------------- 没做过,友情帮顶…… --------------------编程问答-------------------- 3樓的很不錯,我也做了個excel版 的。。
要使用C#操作word,首先要添加引用: 

1、添加引用->COM->Microsoft Excel 11.0 Object Library 

2、在.cs文件中添加 

using Excel ; 

 public void OutputExcel(DataView dv, string str)
    {
  
        #region
        GC.Collect();

        //ExcelOperate excelOperate = new ExcelOperate();
        Application excel;// = new Application();
        //int rowIndex = 4;
        //int colIndex = 1;

        _Workbook xBk;
        _Worksheet xSt;

        excel = new ApplicationClass();

        xBk = excel.Workbooks.Add(Server.MapPath("xls_template") + "\\標准工時模板.xls");

        xSt = (_Worksheet)xBk.ActiveSheet;

        xSt.get_Range(xSt.Cells[2, 1], xSt.Cells[2, 9]).Merge(Missing.Value); //横向合并
        xSt.get_Range(xSt.Cells[2, 1], xSt.Cells[2, 1]).Value2 = GetDept(Session["sa_kb"].ToString()) + "    " + "標准工時";
        //13838
        xSt.Cells[3, 1] = "成品料號:" + Session["sa001"].ToString();
        xSt.Cells[3, 3] = "型號:" + Session["sa02x"].ToString();
        xSt.Cells[3, 8] = "版次/版本:" + Session["banchi"].ToString();
        xSt.Cells[4, 1] = "半成品料號:" + Session["sa011"].ToString();
        xSt.Cells[4, 3] = "訂單:" + Session["sa_NO"].ToString();
        xSt.Cells[4, 8] = "IE:" + Session["sa_IE"].ToString();
        for (int i = 0; i < dv.Count; i++)
        {
            xSt.Cells[7 + i, 1] = dv[i].Row[0].ToString();

            xSt.Cells[7 + i, 2] = dv[i].Row[1].ToString();
            xSt.Cells[7 + i, 3] = dv[i].Row[3].ToString();
            xSt.Cells[7 + i, 4] = dv[i].Row[4].ToString();
            xSt.Cells[7 + i, 5] = dv[i].Row[5].ToString();

            if (dv[i].Row[2].ToString() == "1")//若是有數據變更的則著色表示
            {

                SetBold(xSt, xSt.Cells[7 + i, 1], xSt.Cells[7 + i, 9], 1);

            }



            SetHAlignCenter(xSt, xSt.Cells[7 + i, 6], xSt.Cells[7 + i, 9]);//居中

        }
        int rowNum = 7;
        DataView dvw = GetBiao();
        for (int i = 0; i < dvw.Count; i++)
        {
            int rowAdd = Convert.ToInt32(dvw[i].Row[1].ToString());

            xSt.get_Range(xSt.Cells[rowNum, 6], xSt.Cells[rowNum + rowAdd - 1, 6]).Merge(Missing.Value);
            xSt.get_Range(xSt.Cells[rowNum, 6], xSt.Cells[rowNum + rowAdd - 1, 6]).Value2 = dvw[i].Row[2].ToString();
            xSt.get_Range(xSt.Cells[rowNum, 7], xSt.Cells[rowNum + rowAdd - 1, 7]).Merge(Missing.Value);
            xSt.get_Range(xSt.Cells[rowNum, 7], xSt.Cells[rowNum + rowAdd - 1, 7]).Value2 = dvw[i].Row[3].ToString();
                       rowNum = rowNum + rowAdd;

        }

        xSt.get_Range(xSt.Cells[(int)dv.Count + 7, 1], xSt.Cells[(int)dv.Count + 7, 2]).Merge(Missing.Value);
        xSt.get_Range(xSt.Cells[(int)dv.Count + 7, 1], xSt.Cells[(int)dv.Count + 7, 2]).Value2 = "間接人員";
        #region
        int dvc = (int)dv.Count + 7;
        xSt.Cells[dvc + 1, 1] = "1";
        xSt.Cells[dvc + 2, 1] = "2";
        xSt.Cells[dvc + 3, 1] = "3";
        xSt.Cells[dvc + 1, 2] = "修改";
        xSt.Cells[dvc + 2, 2] = "調機";
        xSt.Cells[dvc + 3, 2] = "備注";
        string[] str1 = GetSum("1").Split(';');
        xSt.Cells[dvc + 1, 3] = str1[0];
        xSt.Cells[dvc + 1, 4] = str1[1];
        xSt.Cells[dvc + 1, 5] = str1[2];
        xSt.Cells[dvc + 1, 6] = str1[3];
        string[] str2 = GetSum("2").Split(';');
        xSt.Cells[dvc + 2, 3] = str2[0];
        xSt.Cells[dvc + 2, 4] = str2[1];
        xSt.Cells[dvc + 2, 5] = str2[2];
        xSt.Cells[dvc + 2, 6] = str2[3];
        string[] str3 = GetSum("3").Split(';');
        xSt.Cells[dvc + 3, 3] = str3[0];
        xSt.Cells[dvc + 3, 4] = str3[1];
        xSt.Cells[dvc + 3, 5] = str3[2];
        xSt.Cells[dvc + 3, 6] = str3[3];
        #endregion

        int ExRow = (int)dv.Count + 12;
        // 將圖片寫入到Excel
        xSt.get_Range(xSt.Cells[ExRow, 6], xSt.Cells[ExRow + 6, 9]).Merge(Missing.Value);
        Worksheet wk = (Worksheet)xSt;
        int heit = ((int)dv.Count) * 22;
        heit = heit - 30;

        string strpath = Session["sa010"].ToString();
        if (strpath != "")
        {
            wk.Shapes.AddPicture(strpath, MsoTriState.msoFalse, MsoTriState.msoTrue, 455, heit, 113, 84);
        }
        xSt.get_Range(xSt.Cells[ExRow, 6], xSt.Cells[ExRow + 6, 9]).Value2 = "";
        xSt.Cells[ExRow, 2] = "產線總人數(人):";
        xSt.Cells[ExRow + 1, 2] = "產線瓶頸時間(秒):";
              //設置位置
        SetHAlignCenter(xSt, xSt.Cells[ExRow, 2], xSt.Cells[ExRow, 2], "str");
        SetHAlignCenter(xSt, xSt.Cells[ExRow + 1, 2], xSt.Cells[ExRow + 1, 2], "str");
               //設置字體產色

        #region
        SetBold(xSt, xSt.Cells[ExRow, 2], xSt.Cells[ExRow, 2], "");
        SetBold(xSt, xSt.Cells[ExRow + 1, 2], xSt.Cells[ExRow + 1, 2], "");
       , 2], "");
        #endregion

        xSt.get_Range(xSt.Cells[ExRow, 3], xSt.Cells[ExRow, 5]).Merge(Missing.Value);
        xSt.get_Range(xSt.Cells[ExRow, 3], xSt.Cells[ExRow, 5]).Value2 = GetSum("4");
        SetBold(xSt, xSt.Cells[ExRow, 3], xSt.Cells[ExRow, 5], "");
        xSt.get_Range(xSt.Cells[ExRow + 1, 3], xSt.Cells[ExRow + 1, 5]).Merge(Missing.Value);
        xSt.get_Range(xSt.Cells[ExRow + 1, 3], xSt.Cells[ExRow + 1, 5]).Value2 = GetSum("5");
       
        
        //居中
        SetHAlignCenter(xSt, xSt.Cells[ExRow, 3], xSt.Cells[ExRow, 5]);
        SetHAlignCenter(xSt, xSt.Cells[ExRow + 1, 3], xSt.Cells[ExRow + 1, 5]);
        SetHAlignCenter(xSt, xSt.Cells[ExRow + 2, 3], xSt.Cells[ExRow + 2, 5]);
        SetHAlignCenter(xSt, xSt.Cells[ExRow + 3, 3], xSt.Cells[ExRow + 3, 5]);
        SetHAlignCenter(xSt, xSt.Cells[ExRow + 4, 3], xSt.Cells[ExRow + 4, 5]);

        xSt.get_Range(xSt.Cells[ExRow + 5, 1], xSt.Cells[ExRow + 5, 5]).Merge(Missing.Value); //横向合并
        xSt.get_Range(xSt.Cells[ExRow + 5, 1], xSt.Cells[ExRow + 5, 5]).Value2 = "注:  適用型號:  ";

        //
        //显示效果
        //
        excel.Visible = false;

        //xSt.Export(Server.MapPath(".")+"\\"+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML);

        string stick = DateTime.Now.Ticks.ToString();
        xBk.SaveCopyAs(Server.MapPath("xls_files") + "\\" + stick + ".xls");

        //ds = null;
        xBk.Close(false, null, null);

        excel.Quit();
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
        xBk = null;
        excel = null;
        xSt = null;
        GC.Collect();
        string path = Server.MapPath("xls_files") + "\\" + stick + ".xls";

        System.IO.FileInfo file = new System.IO.FileInfo(path);
        Response.Clear();
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        // 添加頭信息,,彈出另存為窗口
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //讓瀏覽器顯示下載信息
        Response.AddHeader("Content-Length", file.Length.ToString());

        // 指定返回一個不能被客戶端讀取的流,下載
        Response.ContentType = "application/ms-excel";

        //把文件流下載到客戶端
        Response.WriteFile(file.FullName);
        // 停止頁面的執行

        Response.End();
        #endregion
    } --------------------编程问答-------------------- up --------------------编程问答-------------------- 上面的多数是文字性的,现在是图片,该咋搞呢
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,