提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
Microsoft.Office.Interop.Excel的读取,保存,内容写入和打印。
提示:以下是本篇文章正文内容,下面案例可供参考
首先于Project => Add COM reference 中引入Microsoft Excel 16.0 Object Library
代码如下(示例):
using Microsoft.Office.Interop.Excel;
Application app = new Application();
Workbook workbook = app.Workbooks.Open(@"C:\Users\admin\Desktop\test.xlsx");
//读取工作表,索引由1开始。
Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
//保存原文件
//workbook.Save();
//保存为新的Excel文件
workbook.SaveAs(@"C:\Users\admin\Desktop\test.xlsx");
//结尾记得关闭服务,不然会导致excel在后台开启
workbook.Close();
app.Quit();
代码如下(示例):
using Microsoft.Office.Interop.Excel;
Application app = new Application();
Workbook workbook = app.Workbooks.Open(@"C:\Users\admin\Desktop\test.xlsx");
Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
//索引基本都从1开始
worksheet.Rows[1].Cells[1].Value = "data";
worksheet.Rows[1].Cells[2].Value = 1234;
workbook.Save();
workbook.Close();
app.Quit();
运行结果:
代码如下(示例):
using Microsoft.Office.Interop.Excel;
Application app = new Application();
Workbook workbook = app.Workbooks.Open(@"C:\Users\admin\Desktop\test.xlsx");
Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
worksheet.Rows[1].Cells[1].Font.Size = 48;
workbook.Save();
workbook.Close();
app.Quit();
运行结果:
代码如下(示例):
using Microsoft.Office.Interop.Excel;
Application app = new Application();
Workbook workbook = app.Workbooks.Open(@"C:\Users\kynesyhchen\Desktop\test.xlsx");
Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
worksheet.Range["A1:C1"].Merge();
worksheet.Range["A3:C4"].Merge();
worksheet.Range["A5:C7"].Merge();
workbook.Save();
workbook.Close();
app.Quit();
运行结果:
代码如下(示例):
using Microsoft.Office.Interop.Excel;
Application app = new Application();
Workbook workbook = app.Workbooks.Open(@"C:\Users\kynesyhchen\Desktop\test.xlsx");
Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
//横向分页符号加在单元格上边
worksheet.HPageBreaks.Add(worksheet.Rows[7].Cells[3]);
//纵向分页符号加在单元格左边
worksheet.VPageBreaks.Add(worksheet.Rows[7].Cells[3]);
workbook.Save();
workbook.Close();
app.Quit();
运行结果:
代码如下(示例):
using Microsoft.Office.Interop.Excel;
Application app = new Application();
Workbook workbook = app.Workbooks.Open(@"C:\Users\kynesyhchen\Desktop\test.xlsx");
Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
worksheet.Rows[1].Cells[1].HorizontalAlignment = XlHAlign.xlHAlignGeneral;
worksheet.Rows[3].Cells[1].HorizontalAlignment = XlHAlign.xlHAlignLeft;
worksheet.Rows[5].Cells[1].HorizontalAlignment = XlHAlign.xlHAlignRight;
worksheet.Rows[7].Cells[1].HorizontalAlignment = XlHAlign.xlHAlignCenter;
workbook.Save();
workbook.Close();
app.Quit();
运行结果:
代码如下(示例):
using Microsoft.Office.Interop.Excel;
Application app = new Application();
Workbook workbook = app.Workbooks.Open(@"C:\Users\kynesyhchen\Desktop\test.xlsx");
Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
workbook.PrintOutEx(
1,
2,
2,
false,
"打印机名称",
false,
true,
true);
workbook.Save();
workbook.Close();
app.Quit();
PrintOutEx内参数由上至下分别为:
from: 从第几页开始,默认由第一页开始。
to : 到第几页结束,默认打印到最后一页
copies:打印份数,默认打印一份
preview:是否调用打印预览,false为立即打印
activePrinter:填入打印机名称,指定打印机打印
printToFile:若要打印到文件,请使用 true。 若未指定 PrToFileName,则 Excel 会提示用户输入输 出文件的名称。
collate:若逐份打印多个副本,请使用 true。
prToFileName:如果 printToFile设为 true,则此参数指定要打印到的文件的名称。
ignorePrintAreas:如果要忽略打印区域并打印整个工作簿,则为 true。
//使用默认设置打印
workbook.PrintOutEx();
//只想改变其中参数,其他位置可以用Type.Missing代替选择默认设计
workbook.PrintOutEx(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, false);
之前在使用Spire.XLS免费版处理excel数据时发现有数量限制以及水印问题,所以摸索了一些Microsoft.Office.Interop.Excel用法,