以下是我調(diào)用的關(guān)鍵程序集
using Microsoft.Office.Core;
根據(jù)這個(gè)程序集我進(jìn)行了里面的封裝以便更快捷的編碼得到相應(yīng)的報(bào)表樣式
生成Excel報(bào)表
/// <summary>
/// 創(chuàng)建Excel內(nèi)容并導(dǎo)出
/// </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];
//設(shè)置兩個(gè)單元格列子
SetCell(1, 1, "", 0, null, true, 0, 0, appexcel, range);
SetCell(1, 2, 1, 339, "全國(guó)-上柜月報(bào)表", 0, null, true, 0, 30, appexcel, range);
try
{
//導(dǎo)出和保存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>
/// 設(shè)置一個(gè)不用合并單元格的樣式和內(nèi)容
/// </summary>
/// <param name="row">單元格的行數(shù)</param>
/// <param name="col">單元格的列數(shù)</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(); //設(shè)置單元格的背景色
range.Cells.Interior.Color = Color.LightGray; //設(shè)置單元格的背景色
}
if (fontsize != 0)
{
range.Font.Size = fontsize;
}
if (width != 0)
{
range.ColumnWidth = width;
}
if (height != 0)
{
range.RowHeight = height;
}
range.Merge(true);
}
/// <summary>
/// 設(shè)置一個(gè)需要合并單元格的樣式和內(nèi)容
/// </summary>
/// <param name="row">單元格的行數(shù)</param>
/// <param name="col">單元格的列數(shù)</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; //設(shè)置單元格的背景色
}
if (fontsize != 0)
{
range.Font.Size = fontsize;
}
if (width != 0)
{
range.ColumnWidth = width;
}
if (height != 0)
{
range.RowHeight = height;
}
range.Merge(false);
}
生成word報(bào)表
關(guān)鍵程序集
using Microsoft.Office.Interop.Word;
object filename = path; //文件保存路徑
//創(chuàng)建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("工商協(xié)同信息交流");
WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; //設(shè)置右對(duì)齊
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; //跳出頁眉設(shè)置
WordApp.Selection.ParagraphFormat.LineSpacing = 15f; //設(shè)置文檔的行間距
//移動(dòng)焦點(diǎn)并換行
object count = 14;
object WdLine = WdUnits.wdLine; //換一行;
WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing); //移動(dòng)焦點(diǎn)
// 插入段落
Microsoft.Office.Interop.Word.Paragraph para;
para = WordDoc.Content.Paragraphs.Add(ref Nothing);
//插入一個(gè)段落
para.Range.Text = "";//段落內(nèi)容
para.Range.Font.Size = 30;//段落字體大小
para.Range.Font.Bold = 4;//段落字體粗細(xì)
para.Range.Font.Name = "黑體";//段落字體樣式
para.Range.Font.Color = WdColor.wdColorRed;//段落字體顏色
para.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//段落位置
para.Range.InsertParagraphAfter();//段落后插入相當(dāng)于回車鍵
WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing); //移動(dòng)焦點(diǎn)
//文檔中創(chuàng)建表格
//參數(shù)為表格的行數(shù)和列數(shù),2行3列
Table newTable =
WordDoc.Tables.Add(WordApp.Selection.Range, DDSCModel.Count + 2, 3,
ref Nothing, ref Nothing);
//設(shè)置表格樣式
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;
//填充表格內(nèi)容
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; //設(shè)置單元格中字體為粗體
//合并單元格
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\真龍運(yùn)行數(shù)據(jù)推送項(xiàng)目\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);