以下是我调用的关键程序集
using Microsoft.Office.Core;
根据这个程序集我进行了里面的封装以便更快捷的编码得到相应的报表样式
生成Excel报表
/// <summary>
/// 创建Excel内容并导出
/// </summary>
/// <param name="filepath">路径</param>
/// <returns></returns>
public bool NewExport(string filepath)
{
bool bSuccess = true;
Microsoft.Office.Interop.Excel.Application appexcel = new Microsoft.Office.Interop.Excel.Application();
System.Reflection.Missing miss = System.Reflection.Missing.Value;
appexcel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbookdata = null;
Microsoft.Office.Interop.Excel.Worksheet worksheetdata = null;
Microsoft.Office.Interop.Excel.Range range;
workbookdata = appexcel.Workbooks.Add(true);
//appexcel.Cells[1, 4].Value = "第一行第四列";
appexcel.Visible = false;
appexcel.DisplayAlerts = false;
//初始化range
range = appexcel.Cells[1, 1];
//设置两个单元格列子
SetCell(1, 1, "", 0, null, true, 0, 0, appexcel, range);
SetCell(1, 2, 1, 339, "全国-上柜月报表", 0, null, true, 0, 30, appexcel, range);
try
{
//导出和保存Excel表
workbookdata.SaveAs(filepath, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
workbookdata.Close(false, miss, miss);
appexcel.Workbooks.Close();
appexcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbookdata);
System.Runtime.InteropServices.Marshal.ReleaseComObject(appexcel.Workbooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(appexcel);
GC.Collect();
}
catch (Exception ex)
{
string ErrorMsg = ex.Message;
Console.WriteLine(ErrorMsg);
MessageBox.Show(ErrorMsg);
bSuccess = false;
}
MessageBox.Show(bSuccess.ToString());
return bSuccess;
}
/// <summary>
/// 设置一个不用合并单元格的样式和内容
/// </summary>
/// <param name="row">单元格的行数</param>
/// <param name="col">单元格的列数</param>
/// <param name="text">单元格文本</param>
/// <param name="fontsize">单元格字体大小</param>
/// <param name="fontname">单元格字体样式</param>
/// <param name="color">单元格是否要背景色</param>
/// <param name="width">单元格宽度</param>
/// <param name="height">单元格高度</param>
/// <param name="appexcel"></param>
/// <param name="range"></param>
private void SetCell(int row, int col, string text, int fontsize, string fontname, bool color, int width, int height, Microsoft.Office.Interop.Excel.Application appexcel, Microsoft.Office.Interop.Excel.Range range)
{
range = appexcel.Cells[row, col];
range.Value = text;
range.Borders.LineStyle = 1;
range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
range.EntireColumn.AutoFit();
if (fontname != null)
{
range.Font.Name = fontname;
}
if (color == true)
{
//range.Cells.Interior.Color = Color.FromArgb(r1, r2, r3).ToArgb(); //设置单元格的背景色
range.Cells.Interior.Color = Color.LightGray; //设置单元格的背景色
}
if (fontsize != 0)
{
range.Font.Size = fontsize;
}
if (width != 0)
{
range.ColumnWidth = width;
}
if (height != 0)
{
range.RowHeight = height;
}
range.Merge(true);
}
/// <summary>
/// 设置一个需要合并单元格的样式和内容
/// </summary>
/// <param name="row">单元格的行数</param>
/// <param name="col">单元格的列数</param>
/// <param name="row2"></param>
/// <param name="col2"></param>
/// <param name="text">单元格文本</param>
/// <param name="fontsize">单元格字体大小</param>
/// <param name="fontname">单元格字体样式</param>
/// <param name="color">单元格是否要背景色</param>
/// <param name="width">单元格宽度</param>
/// <param name="height">单元格高度</param>
/// <param name="appexcel"></param>
/// <param name="range"></param>
private void SetCell(int row, int col, int row2, int col2, string text, int fontsize, string fontname, bool color, int width, int height, Microsoft.Office.Interop.Excel.Application appexcel, Microsoft.Office.Interop.Excel.Range range)
{
range = appexcel.Range[appexcel.Cells[row, col], appexcel.Cells[row2, col2]];
range.Value = text;
range.Borders.LineStyle = 1;
range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
range.EntireColumn.AutoFit();
if (fontname != null)
{
range.Font.Name = fontname;
}
if (color == true)
{
range.Cells.Interior.Color = Color.LightGray; //设置单元格的背景色
}
if (fontsize != 0)
{
range.Font.Size = fontsize;
}
if (width != 0)
{
range.ColumnWidth = width;
}
if (height != 0)
{
range.RowHeight = height;
}
range.Merge(false);
}
生成word报表
关键程序集
using Microsoft.Office.Interop.Word;
object filename = path; //文件保存路径
//创建Word文档
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing,
ref Nothing, ref Nothing);
//添加页眉
WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("工商协同信息交流");
WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; //设置右对齐
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; //跳出页眉设置
WordApp.Selection.ParagraphFormat.LineSpacing = 15f; //设置文档的行间距
//移动焦点并换行
object count = 14;
object WdLine = WdUnits.wdLine; //换一行;
WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing); //移动焦点
// 插入段落
Microsoft.Office.Interop.Word.Paragraph para;
para = WordDoc.Content.Paragraphs.Add(ref Nothing);
//插入一个段落
para.Range.Text = "";//段落内容
para.Range.Font.Size = 30;//段落字体大小
para.Range.Font.Bold = 4;//段落字体粗细
para.Range.Font.Name = "黑体";//段落字体样式
para.Range.Font.Color = WdColor.wdColorRed;//段落字体颜色
para.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//段落位置
para.Range.InsertParagraphAfter();//段落后插入相当于回车键
WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing); //移动焦点
//文档中创建表格
//参数为表格的行数和列数,2行3列
Table newTable =
WordDoc.Tables.Add(WordApp.Selection.Range, DDSCModel.Count + 2, 3,
ref Nothing, ref Nothing);
//设置表格样式
newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleThickThinLargeGap;
newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
newTable.Columns[1].Width = 160f;//每一列的的宽度
newTable.Columns[2].Width = 160f;
newTable.Columns[3].Width = 160f;
//填充表格内容
newTable.Cell(1, 1).Range.Text = "单位:箱";
newTable.Cell(1, 1).Range.Font.Color = WdColor.wdColorBlack;
newTable.Cell(1, 1).Range.Font.Size = 10;
newTable.Cell(1, 1).Range.Bold = 1; //设置单元格中字体为粗体
//合并单元格
newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
WordApp.Selection.Cells.VerticalAlignment =
WdCellVerticalAlignment.wdCellAlignVerticalCenter; //垂直居中
WordApp.Selection.ParagraphFormat.Alignment =
WdParagraphAlignment.wdAlignParagraphLeft; //水平居中
//插入图片
string FileName = @"D:\SVN2\真龙运行数据推送项目\DataSend\DataSend.Word\3.png"; //图片所在路径
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = WordDoc.Application.Selection.Range;
//文件保存
WordDoc.Application.ActiveDocument.Shapes.AddPicture(FileName, ref LinkToFile,
ref SaveWithDocument, 0f, 0f, 400f, 500f);
WordDoc.SaveAs(ref filename, 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);
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);