vs使用.net简单生成Word、Excel文件

1、首先引入
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Excel;
using System.IO;
using System.Reflection;命名空间。
2、生成Excel表格

private void button2_Click(object sender, EventArgs e)
        {
            object path = @"D:\测试\MyText.xlsx";

            if (File.Exists((string)path))//检测指定的文件是否存在(需要using System.IO)
            {
                File.Delete((string)path);//存在就删除
            }

            Microsoft.Office.Interop.Excel.Application appExcel;   //EXCEL应用程序变量
            Workbook excelDoc;      //EXCEL文档变量

            //初始化 vs2010用Application()
            appExcel = new Microsoft.Office.Interop.Excel.Application();   //无法嵌入互操作类型(引用的EXCEL中嵌入操作改为False)

            //由于使用COM库,因此有很多变量需要使用Nothing代替
            Object Nothing = Missing.Value;
            excelDoc = appExcel.Workbooks.Add(Nothing);

            //WdSaveFormat为EXCEL文档的格式保存
            object format = XlFileFormat.xlWorkbookNormal;

            //使用第一个工作表作为插入数据的工作表
            Worksheet ws = (Worksheet)excelDoc.Sheets[1];

            //在指定地方赋值 
            Microsoft.Office.Interop.Excel.Range r = ws.get_Range("A1", "A1"); //获取第1行第1列单元格
            r.Value2 = "afdafa";                          //向第1行第1列单元格赋
            //将excelDoc文档对象的内容保存为XLSX文档
            excelDoc.SaveAs(path, Nothing, Nothing, Nothing, Nothing, Nothing, XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);
            //关闭excelDoc文档对象 
            excelDoc.Close(Nothing, Nothing, Nothing);
            //关闭excelApp组件对象
            appExcel.Quit();
        }

3、生成word表格

private void button1_Click(object sender, EventArgs e)
        {
            string filePath = @"D:\测试\1321.doc";
            //初始化wordApp组件
            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
            Document wordDoc;//创建文档对象
            if (File.Exists((string)filePath))//检测指定的文件是否存在(需要using System.IO)
            {
                File.Delete((string)filePath);//存在就删除
            }

            Object nothing = Missing.Value;
            wordDoc = wordApp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);

            /* 1、写入普通文本   */
            string content = "Hello!"+Environment.NewLine;
            wordDoc.Paragraphs.Last.Range.Text = content;
            object format = WdSaveFormat.wdFormatDocument97;

            /* 2、 写入需要的特殊格式文本 */
            //写入15号字体
            content = "这一行是15号字体的文本"+Environment.NewLine;
            wordDoc.Paragraphs.Last.Range.Font.Size = 15;
            wordDoc.Paragraphs.Last.Range.Text = content;

            //写入斜体文本
            content = "这一行是斜体文本"+Environment.NewLine;
            wordDoc.Paragraphs.Last.Range.Font.Italic = 1;
            wordDoc.Paragraphs.Last.Range.Text = content;

            //写入红色下划线文本
            content = "这一行是红色下划线的文本"+Environment.NewLine;
            wordDoc.Paragraphs.Last.Range.Font.Underline = WdUnderline.wdUnderlineThick;
            wordDoc.Paragraphs.Last.Range.Font.UnderlineColor = WdColor.wdColorRed;
            wordDoc.Paragraphs.Last.Range.Text = content;

            /* 3、写入表格   */
            //表格对象
            Table table = wordDoc.Tables.Add(wordApp.Selection.Range, 5,5, ref nothing, ref nothing);
            table.Borders.Enable = 1;
            for (int i = 1;i < 6; i++)
{
                for (int j = 1; j < 6; j++)
                {
                    table.Cell(i, j).Range.Text = "" + i + "行"+" "+j+"列";
                }
            }

            /* 4、插入图片   */
            string jpgName = @"C:\Users\x3626\Pictures\Camera Roll\090536vzc9e7lxjaulp9ue.jpg";
            Object range = wordDoc.Paragraphs.Last.Range;
            Object linkToFile = false;
            Object saveWithDocument = true;
            wordDoc.InlineShapes.AddPicture(jpgName, ref linkToFile, ref saveWithDocument, ref range);

            wordDoc.SaveAs(filePath);//保存word文档
            wordDoc.Close(ref nothing, ref nothing, ref nothing);//关闭WordDoc文档对象
            wordApp.Quit(ref nothing, ref nothing, ref nothing);//关闭WordApp组件对象
        }

喜欢就点个赞吧

猜你喜欢

转载自blog.csdn.net/weixin_44790046/article/details/103913656