用C#把数据库中的记录按格式写入Word文档中。

使用Word的COM组件。

Word._Application app = new Word.ApplicationClass();   
app.Visible = false;
    
object nothing = System.Reflection.Missing.Value;
object temp = Environment.CurrentDirectory + @"/resume.doc";   //文档模板

Word.Document doc = app.Documents.Open(ref temp,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);//打开模板

doc.Tables[1].Cell(1,2).Range.Text  = ""; // 给单元格添加内容

//给单元格插入图片
doc.Tables[1].Cell(1,5).Range.InlineShapes.AddPicture(ImgPath,ref nothing,ref nothing,ref nothing);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用C#的SerialPort类来连接扫描枪,并使用Excel Interop或者一些第三方库来将读取到的内容写入Excel文档。 以下是一个简单的示例代码,仅供参考: ```csharp using System; using System.IO.Ports; using Excel = Microsoft.Office.Interop.Excel; namespace ScanGunExcel { class Program { static SerialPort _serialPort; static Excel.Application _excelApp; static Excel.Workbook _workbook; static Excel.Worksheet _worksheet; static int _row = 1; static void Main(string[] args) { // 初始化串口连接 _serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); _serialPort.DataReceived += SerialPort_DataReceived; _serialPort.Open(); // 初始化Excel文档 _excelApp = new Excel.Application(); _workbook = _excelApp.Workbooks.Add(); _worksheet = _workbook.Worksheets[1]; _worksheet.Cells[1, 1] = "ScanGun Content"; Console.WriteLine("Press any key to stop."); Console.ReadKey(); // 关闭串口连接和Excel文档 _serialPort.Close(); _workbook.SaveAs("ScanGun.xlsx"); _excelApp.Quit(); } private static void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { string content = _serialPort.ReadLine().Trim(); // 写入Excel文档 _worksheet.Cells[_row, 1] = content; _row++; } } } ``` 在这个示例,我们使用SerialPort类来连接扫描枪。每当扫描枪读取到内容时,SerialPort_DataReceived事件会被触发,我们将读取到的内容写入Excel文档。最后,我们通过调用Excel Interop的API来保存Excel文档,并关闭Excel应用程序。 需要注意的是,使用Excel Interop可能需要你安装Microsoft Office软件,并且在使用完Excel对象后需要释放资源,否则可能会导致内存泄漏。另外,如果你不想使用Excel Interop,你也可以考虑使用一些第三方库,例如EPPlus或者NPOI来操作Excel文档
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值