C#利用Microsoft.Office.Interop.Excel導出數據到Excel

添加引用(VS2013位於C:\Program Files (x86)\Microsoft Visual Studio 12.0\Visual Studio Tools for Office\PIA\Office15文件夾下)

本案例是直接將DataTable裏面的數據導入到Excel內,因此需將數據轉換成DataTable數據類型才能夠web

using Microsoft.Office.Interop.Excel;
using System.Reflection;
public void DataTableToExcel(System.Data.DataTable dataTable, string strTitle, string[] strHead, int[] columnWidth)
{
	int nMax = dataTable.Rows.Count;
	int nMin = 0;
	int rowCount = nMax - nMin + 1;//總行數
	const int columnCount = 7;//總列數

	//建立Excel對象
	Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
	//新建工做簿
	Workbook workBook = excelApp.Workbooks.Add(true);
	//新建工做表
	Worksheet worksheet = workBook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;

	////設置標題
	Microsoft.Office.Interop.Excel.Range titleRange = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[1, columnCount]];//選取單元格

	titleRange.Merge(true);                                         //合併單元格
	titleRange.Value2 = strTitle;                                   //設置單元格內文本
	titleRange.Font.Name = "微軟雅黑";                              //設置字體
	titleRange.Font.Size = 18;                                      //字體大小
	titleRange.Font.Bold = true;                                    //加粗顯示
	titleRange.HorizontalAlignment = XlHAlign.xlHAlignCenter;       //水平居中
	titleRange.VerticalAlignment = XlVAlign.xlVAlignCenter;         //垂直居中
	titleRange.Borders.LineStyle = XlLineStyle.xlContinuous;        //設置邊框
	titleRange.Borders.Weight = XlBorderWeight.xlThin;              //邊框常規粗細

	//設置表頭
	//string[] strHead = new string[columnCount] { "id", "productUid", "productBarcode", "checkData", "checkResult", "creater", "checkDate" };
	//int[] columnWidth = new int[columnCount] { 8, 24, 24, 22, 10, 14, 20 };
	for (int i = 0; i < columnCount; i++)
	{
		Microsoft.Office.Interop.Excel.Range headRange = worksheet.Cells[2, i + 1] as Microsoft.Office.Interop.Excel.Range;//獲取表頭單元格,不用標題則從1開始
		headRange.Value2 = strHead[i];                              //設置單元格文本
		headRange.Font.Name = "Consolas";                           //設置字體
		headRange.Font.Size = 13;                                   //字體大小
		headRange.Font.Bold = true;                                 //加粗顯示
		headRange.HorizontalAlignment = XlHAlign.xlHAlignCenter;    //水平居中
		headRange.VerticalAlignment = XlVAlign.xlVAlignCenter;      //垂直居中
		headRange.ColumnWidth = columnWidth[i];                     //設置列寬
		headRange.Borders.LineStyle = XlLineStyle.xlContinuous;     //設置邊框
		headRange.Borders.Weight = XlBorderWeight.xlThin;           //邊框常規粗細
	}

	//設置每列格式
	for (int i = 0; i < columnCount; i++)
	{
		Microsoft.Office.Interop.Excel.Range contentRange = worksheet.Range[worksheet.Cells[2, i + 1], worksheet.Cells[rowCount - 1 + 3, i + 1]];//不用標題則從第二行開始
		contentRange.Font.Name = "Consolas";
		contentRange.Font.Size = 10;
		contentRange.HorizontalAlignment = XlHAlign.xlHAlignCenter; //水平居中
		contentRange.VerticalAlignment = XlVAlign.xlVAlignCenter;   //垂直居中
		contentRange.Borders.LineStyle = XlLineStyle.xlContinuous;  //設置邊框
		contentRange.Borders.Weight = XlBorderWeight.xlThin;        //邊框常規粗細
		contentRange.WrapText = true;                               //自動換行
		contentRange.NumberFormatLocal = "@";                       //文本格式
	}

	//填充數據
	for (int i = 0; i < dataTable.Rows.Count; i++)
	{
		DataRow row = dataTable.Rows[i];
		for (int j = 0; j < dataTable.Columns.Count; j++)
		{
			excelApp.Cells[i + 3, j + 1] = row[j].ToString();
		}
	}
	//設置Excel可見
	excelApp.Visible = true;
}
相關文章
相關標籤/搜索