1.引用 Microsoft.Office.Interop.Excel 程序集 (可以直接在Vs的引用—>管理nuget程序包—>搜索office,安装即可)
2.废话不多说先上代码
//在将要导出数据的地方打开excel并生成表格(表格可以预设好一些内容,比如标题行等)
Application appExcel = new Application();
Microsoft.Office.Interop.Excel.Workbook _workBook = ValueToExcel.CreateNewExcelFile(appExcel);
Sheets _sheets = ValueToExcel.GetWorkSheet(_workBook);
Microsoft.Office.Interop.Excel.Worksheet _workSheet1 = ValueToExcel.GetFirstSheet(_sheets);
///————————————-分割————————–分割————————-分割—————————-分割——————-
下边为写好的方法,我会在需要用到的地方加强注释,可以直接复制使用
using Microsoft.Office.Interop.Excel;
using System.Reflection;
namespace test
{
/// <summary>
/// Excel文件操作类及方法
/// </summary>
class ValueToExcel
{
/// <summary>
/// 创建一个新的Excel文档
/// </summary>
/// <param name=\”app\”></param>
/// <returns></returns>
public static Workbook CreateNewExcelFile(Application app)
{
app.Visible = true;
Workbooks _workbooks = app.Workbooks;
Workbook _workbook = _workbooks.Add(true);
return _workbook;
}
public static Sheets GetWorkSheet(Workbook _workbook)
{
Sheets _sheets = _workbook.Sheets;
_sheets.Add();
return _sheets;
}
/// <summary>
/// 获取Excel文档中的第一张工作表
/// </summary>
/// <param name=\”_sheets\”></param>
/// <returns></returns>
public static Worksheet GetFirstSheet(Sheets _sheets)
{
Worksheet _worksheet = (Worksheet)_sheets.get_Item(1);
_worksheet.Name = \”表格名称\”; //工作表名称
//设置Excel文档中工作表的列宽度
((Range)_worksheet.Columns[1, Missing.Value]).ColumnWidth = 25; //第一列
((Range)_worksheet.Columns[2, Missing.Value]).ColumnWidth = 12; //第二列
((Range)_worksheet.Columns[3, Missing.Value]).ColumnWidth = 25; //类推。。。。
((Range)_worksheet.Columns[4, Missing.Value]).ColumnWidth = 12; //
((Range)_worksheet.Columns[5, Missing.Value]).ColumnWidth = 12; //
((Range)_worksheet.Columns[6, Missing.Value]).ColumnWidth = 12; //
((Range)_worksheet.Columns[7, Missing.Value]).ColumnWidth = 12; //
//设置Excel文档中工作表的行高度
((Range)_worksheet.Rows[1, Missing.Value]).RowHeight = 35; //第一行
((Range)_worksheet.Rows[2, Missing.Value]).RowHeight = 30; //第二行
((Range)_worksheet.Rows[3, Missing.Value]).RowHeight = 30; //
((Range)_worksheet.Rows[4, Missing.Value]).RowHeight = 30; //
((Range)_worksheet.Rows[5, Missing.Value]).RowHeight = 30; //
((Range)_worksheet.Rows[6, Missing.Value]).RowHeight = 30; //
((Range)_worksheet.Rows[7, Missing.Value]).RowHeight = 30; //
((Range)_worksheet.Rows[8, Missing.Value]).RowHeight = 30; //
((Range)_worksheet.Rows[9, Missing.Value]).RowHeight = 30; //
((Range)_worksheet.Rows[10, Missing.Value]).RowHeight = 30; //
//设置第一列的A1到A10 内容超出自动换行
_worksheet.Range[\”A1\”,\”A10\”].WrapText = true;
//设置单元格子的内外边框
SetRowBorderLine(_worksheet,1);
SetRowBorderLine(_worksheet, 2);
SetRowBorderLine(_worksheet, 3);
SetRowBorderLine(_worksheet, 4);
SetRowBorderLine(_worksheet, 5);
SetRowBorderLine(_worksheet, 6);
SetRowBorderLine(_worksheet, 7);
SetRowBorderLine(_worksheet, 8);
SetRowBorderLine(_worksheet, 9);
SetRowBorderLine(_worksheet, 10);
SetCellAlignment(_worksheet,1);
SetCellAlignment(_worksheet, 2);
SetCellAlignment(_worksheet, 3);
SetCellAlignment(_worksheet, 4);
SetCellAlignment(_worksheet, 5);
SetCellAlignment(_worksheet, 6);
SetCellAlignment(_worksheet, 7);
SetCellAlignment(_worksheet, 8);
SetCellAlignment(_worksheet, 9);
SetCellAlignment(_worksheet, 10);
ValueToExcel.SetRowBorderLine(_worksheet, 1);
ValueToExcel.SetCellAlignment(_worksheet, 1);
//填充内容的时候用的写好的 ValueToExcel.FillContentInCell 方法,动态填充内容的时候,在执行代码中进行填充
//例如,使用foreach语句,将获取好的数据组遍历,设置好其实的 int row (行),和 int column(列)
第一列保持不变,每循环一次row++,就能在第一列将数据一行一行填充(然后举一反三。。。)
//设置Excel文件中第一列的文字
ValueToExcel.FillContentInCell(_worksheet, 2, 1, \”1\”); //
ValueToExcel.FillContentInCell(_worksheet, 3, 1, \”2\”); //
ValueToExcel.FillContentInCell(_worksheet, 4, 1, \”3\”);
ValueToExcel.FillContentInCell(_worksheet, 5, 1, \”4\”);
ValueToExcel.FillContentInCell(_worksheet, 6, 1, \”5\”);
ValueToExcel.FillContentInCell(_worksheet, 7, 1, \”6\”);
ValueToExcel.FillContentInCell(_worksheet, 8, 1, \”7\”);
ValueToExcel.FillContentInCell(_worksheet, 9, 1, \”8\”);
ValueToExcel.FillContentInCell(_worksheet, 10, 1, \”9\”);
//设置Excel文件中第一行的文字
ValueToExcel.FillContentInCell(_worksheet, 1, 1, \”一\”);
ValueToExcel.FillContentInCell(_worksheet, 1, 2, \”二\”);
ValueToExcel.FillContentInCell(_worksheet, 1, 3, \”三\”);
ValueToExcel.FillContentInCell(_worksheet, 1, 4, \”四\”);
ValueToExcel.FillContentInCell(_worksheet, 1, 5, \”五\”);
ValueToExcel.FillContentInCell(_worksheet, 1, 6, \”六\”);
ValueToExcel.FillContentInCell(_worksheet, 1, 7, \”七\”);
return _worksheet;
}
/// <summary>
/// 设置Excel文档单元格的内容
/// </summary>
/// <param name=\”wsh\”>Excel文档的当前Sheet</param>
/// <param name=\”row\”>行</param>
/// <param name=\”column\”>列</param>
/// <param name=\”str\”>单元格内容</param>
public static void FillContentInCell(Worksheet _worksheet, int row, int column, string str)
{
_worksheet.Cells[row, column] = str;
}
/// <summary>
/// 设置Excel文档某一行的颜色
/// </summary>
/// <param name=\”wsh\”>Excel文档的当前Sheet</param>
/// <param name=\”row\”>行</param>
/// <param name=\”rbg\”>Excel颜色</param>
public static void SetRowColour(Worksheet _worksheet, int row, int rbg)
{
Range range = (Range)_worksheet.get_Range(\”A\” + row.ToString(), \”G\” + row.ToString()); //从A列到F列
range.Interior.ColorIndex = rbg;
}
/// <summary>
/// 设置单元格边框
/// </summary>
/// <param name=\”_worksheet\”></param>
/// <param name=\”row\”></param>
public static void SetRowBorderLine(Worksheet _worksheet, int row)
{
Range range = (Range)_worksheet.get_Range(\”A\” + row.ToString(), \”G\” + row.ToString()); //从A列到F列
range.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; //设置边框
range.Borders.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin; //边框常规粗细
}
public static void SetCellAlignment(Worksheet _worksheet, int row)
{
Range range = (Range)_worksheet.get_Range(\”A\” + row.ToString(), \”G\” + row.ToString()); //从A列到F列
range.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter; //垂直居中
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //水平居中
}
/// <summary>
/// 保存并关闭Excel文档
/// </summary>
/// <param name=\”app\”></param>
/// <param name=\”wbk\”></param>
public static void SaveAndCloseExcelFile(Application app, Workbook wbk)
{
wbk.Save();
wbk.Close();
app.Workbooks.Close();
app.Application.Quit();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
wbk = null;
app = null;
}
}
}
///————————————-分割————————–分割————————-分割—————————-分割——————-
//整体写好的方法还有待完善,但是够用,希望能帮到你