7,765
社区成员
发帖
与我相关
我的任务
分享
public class ExportExcelFile
{
#region 直接调用ExportExcel()方法
private Excel.Workbooks m_objBooks = null;
private Excel._Workbook m_objBook = null;
private Excel.Sheets m_objSheets = null;
private Excel._Worksheet m_objSheet = null;
private Excel.Application xlsApp = null;
private Excel.Range xlsRan = null;
private object m_objOpt = System.Reflection.Missing.Value;
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="tb">数据表</param>
/// <param name="picPath1">图片路径1</param>
/// <param name="picPath2">图片路径2</param>
/// <param name="picPath3">图片路径3</param>
/// <param name="testInfo">考试信息</param>
/// <param name="testBatch">考试批次</param>
/// <param name="targetPath">导出路径</param>
public void ExportExcel(System.Data.DataTable tb, string picPath1, string picPath2, string picPath3, string testInfo, string testBatch, string targetPath)
{
xlsApp = new Excel.Application();
//xlsApp.Visible = true;
if (xlsApp == null)
{
MessageBox.Show("不能打开Excel!");
return;
}
xlsApp.Application.Workbooks.Add(true);
//初始化
m_objBooks = (Excel.Workbooks)xlsApp.Workbooks;
m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));
m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
SetExcelStyle();//设置表格样式
#region 赋值
m_objSheet.Cells[1, 1] = testBatch;
m_objSheet.Cells[3, 1] = testInfo;
int count = tb.Rows.Count;
int column = tb.Columns.Count;
for (int i = 0; i < column; i++)
{
m_objSheet.Cells[22, i + 1] = tb.Columns[i].ColumnName;
}
for (int i = 0; i < count; i++)
{
for (int j = 0; j < column; j++)
{
m_objSheet.Cells[i + 23, j + 1] = tb.Rows[i][j];
}
}
xlsRan = m_objSheet.get_Range(xlsApp.Cells[22, 1], xlsApp.Cells[22 + count, column]);
xlsRan.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThick, Excel.XlColorIndex.xlColorIndexAutomatic, Color.Yellow.ToArgb());
#endregion
#region 插入图片
xlsRan = m_objSheet.get_Range(xlsApp.Cells[9, 2], xlsApp.Cells[19, 5]);
xlsRan.MergeCells = true;
InsertImage(picPath1, xlsRan);
xlsRan = m_objSheet.get_Range(xlsApp.Cells[9, 7], xlsApp.Cells[19, 10]);
xlsRan.MergeCells = true;
InsertImage(picPath2, xlsRan);
xlsRan = m_objSheet.get_Range(xlsApp.Cells[9, 12], xlsApp.Cells[19, 15]);
xlsRan.MergeCells = true;
InsertImage(picPath3, xlsRan);
#endregion
//保存文件
SaveExcel(targetPath);
xlsApp.Quit();
xlsApp = null;
Dispose();
}
/// <summary>
/// 设置表格样式
/// </summary>
/// <param name="xlsRan"></param>
private void SetExcelStyle()
{
xlsRan = m_objSheet.get_Range(xlsApp.Cells[1, 1], xlsApp.Cells[1, 16]);
xlsRan.MergeCells = true;
xlsRan.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
xlsRan.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThick, Excel.XlColorIndex.xlColorIndexAutomatic, Color.Yellow.ToArgb());
xlsRan = m_objSheet.get_Range(xlsApp.Cells[3, 1], xlsApp.Cells[6, 16]);
xlsRan.MergeCells = true;
xlsRan.WrapText = true;//文本自动换行
xlsRan.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThick, Excel.XlColorIndex.xlColorIndexAutomatic, Color.Yellow.ToArgb());
xlsRan = m_objSheet.get_Range(xlsApp.Cells[8, 1], xlsApp.Cells[20, 16]);
xlsRan.MergeCells = true;
xlsRan = m_objSheet.get_Range(xlsApp.Cells[8, 1], xlsApp.Cells[20, 16]);
xlsRan.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThick, Excel.XlColorIndex.xlColorIndexAutomatic, Color.Yellow.ToArgb());
Excel.XlBorderWeight.xlThick, Excel.XlColorIndex.xlColorIndexAutomatic, Color.Yellow.ToArgb());
}
/// <summary>
/// 插入图片
/// </summary>
private void InsertImage(string picPath, Excel.Range er)
{
er.Select();
float PicLeft, PicTop;
PicLeft = Convert.ToSingle(er.Left);
PicTop = Convert.ToSingle(er.Top);
float width = Convert.ToSingle(er.Width);
float heigth = Convert.ToSingle(er.Height);
m_objSheet.Shapes.AddPicture(picPath, Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoCTrue, PicLeft, PicTop, width, heigth);
}
/// <summary>
/// 保存文件
/// </summary>
/// <param name="targetPath"></param>
private void SaveExcel(string targetPath)
{
m_objBook.SaveAs(targetPath, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);
//MessageBox.Show("文件保存在" + targetPath);
}
/// <summary>
/// 释放所有引用的COM对象。
/// </summary>
public void Dispose()
{
ReleaseObj(m_objSheets);
ReleaseObj(m_objBook);
ReleaseObj(m_objBooks);
ReleaseObj(xlsApp);
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
}
/// <summary>
/// 释放对象
/// </summary>
/// <param name="o"></param>
private void ReleaseObj(object o)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
}
catch
{
}
finally
{
o = null;
}
}
#endregion
}