首先引用:
COM中的 Microsoft Excel 14.0 Object Library
接着引用:
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;

1 public void ExecutePrint(Student objStudent) {
2 //定义一个Excel工作簿
3 Microsoft.Office.Interop.Excel.Application
4 excelApp = new Application();
5 //获取已创建好的工作簿路径
6 string excelBookPath = Environment.CurrentDirectory + "\\StudentInfo.xls";
7 //将现有工作簿加入已定义的工作簿集合
8 excelApp.Workbooks.Add(excelBookPath);
9 //获取第一个工作表
10 Worksheet objSheet = (Worksheet)excelApp.Worksheets[1];
11 //在当前的Excel中写入数据
12 if(objStudent.StuImage.Length!=0) {
13 //将图片保存在指定的位置
14 Image objImage = (Image)new Common.SerializeObjectToString().DeserializeObject(objStudent.StuImage);//把字符串转化为Image 对象
15 if (File.Exists(Environment.CurrentDirectory + "\\Student.jpg"))
16 File.Delete(Environment.CurrentDirectory + "\\Student.jpg");
17 else {
18 //保存图片到系统目录(当前会保存在Debug或者Release文件中)
19 objImage.Save(Environment.CurrentDirectory + "\\Student.jgp");
20 //将图片插入到Excel
21 objSheet.Shapes.AddPicture(Environment.CurrentDirectory + "\\Student.jpg", MsoTriState.msoFalse, MsoTriState.msoCTrue, 10, 50, 70, 80);
22 //使用完毕后删除保存的图片
23 File.Delete(Environment.CurrentDirectory + "\\Student.jgp");
24 }
25 }
26 objSheet.Cells[4, 4] = objStudent.StudentId;
27 objSheet.Cells[4, 6] = objStudent.StudentName;
28 objSheet.Cells[4, 8] = objStudent.Gender;
29 objSheet.Cells[6, 4] = objStudent.ClassName;
30 objSheet.Cells[6, 6] = objStudent.PhoneNumber;
31 objSheet.Cells[8, 4] = objStudent.StudentAddress;
32 //打印预览
33 excelApp.Visible = true;
34 excelApp.Sheets.PrintPreview(true);
35 //释放对象
36 System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);//释放
37 excelApp = null;
38 }
打印结果
来源:https://www.cnblogs.com/zgrh/p/11139711.html
