通过Excel模板打印

附代码
public void ExcelPrint(Students stuExt)
{
//1、创建一个Excel对象
Application excelApp = new Application();
//获取已创建好的工作簿非相片路径
string ExcelPatch = Environment.CurrentDirectory + "\StudentInfo.xls";
//将现有的工作簿加入定义的集合当去
excelApp.Workbooks.Add(ExcelPatch);
//获取第一个工作表
Worksheet sheet = excelApp.Worksheets[1];
//判断相片是否存在
string imagePatch = @"C:\Users\caiwenwen\Desktop\C#程序\Students_Info_Manage_Sys\bin\Debug\Student.jpg";
if (stuExt.StuImage.Length!=0)
{
//首先进行照片的反序列化
//同一命名空间无需要写命名空间
Image ige = (Image)new SerializeObjectToString().DeserializeObject(stuExt.StuImage);

            //判断这个照片是否存在
            if (File.Exists(imagePatch))
            {
                File.Delete(imagePatch);
            }
            else
            {
                ige.Save(imagePatch);
                //将图片加载在对应的工作簿中
                sheet.Shapes.AddPicture(imagePatch, Microsoft.Office.Core.MsoTriState.msoFalse,
                    Microsoft.Office.Core.MsoTriState.msoCTrue, 10, 50, 90, 100);
                //使用后删除照片
               
            }

        }
        File.Delete(imagePatch);
        sheet.Cells[4, 4] = stuExt.StudentId;
        sheet.Cells[4, 6] = stuExt.StudentName;
        sheet.Cells[4, 8] = stuExt.Gender;
        sheet.Cells[6, 4] = stuExt.ClassName;
        sheet.Cells[6, 6] = stuExt.PhoneNumber;
        sheet.Cells[8, 4] = stuExt.StudentAddress;

        //备注信息
        excelApp.Visible = true;
        excelApp.Sheets.PrintPreview(true);

        //释放对象
        excelApp.Quit();
        System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
        excelApp = null;
    }
posted @ 2021-11-22 23:46  跨界专家  阅读(170)  评论(0)    收藏  举报