c#高效率導(dǎo)出多維表頭excel的實(shí)例代碼
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
private void ExportToExcel(string fielName)
{
//實(shí)例化一個(gè)Excel.Application對(duì)象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
try
{
if (dgv_Result.DataSource == null)
return;
if (dgv_Result.Rows.Count == 0)
return;
//新增加一個(gè)工作簿,Workbook是直接保存,不會(huì)彈出保存對(duì)話框,加上Application會(huì)彈出保存對(duì)話框,值為false會(huì)報(bào)錯(cuò)
Microsoft.Office.Interop.Excel.Workbook xlBook = excel.Workbooks.Add(true);
//1.添加表頭
excel.Cells[1, 1] = tyclass;
for (int i = 0; i < dgv_Result.Columns.Count; i++)
{
excel.Cells[2, i + 1] = dgv_Result.Columns[i].Name;
}
#region 2.實(shí)現(xiàn)Excel多維表頭 采用合并單元格的方式
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)xlBook.ActiveSheet;
Microsoft.Office.Interop.Excel.Range excelRange = sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[1, 2]);
Microsoft.Office.Interop.Excel.Range excelRange1 = sheet.get_Range(sheet.Cells[1, 3], sheet.Cells[1, 4]);
Microsoft.Office.Interop.Excel.Range excelRange2 = sheet.get_Range(sheet.Cells[1,5], sheet.Cells[1, 6]);
Microsoft.Office.Interop.Excel.Range excelRange3 = sheet.get_Range(sheet.Cells[1,7], sheet.Cells[1, 8]);
Microsoft.Office.Interop.Excel.Range excelRange4 = sheet.get_Range(sheet.Cells[1, 2], sheet.Cells[1, 3]);
Microsoft.Office.Interop.Excel.Range excelRange5 = sheet.get_Range(sheet.Cells[1, 6], sheet.Cells[1, 7]);
Microsoft.Office.Interop.Excel.Range excelRange6 = sheet.get_Range(sheet.Cells[1, 4], sheet.Cells[1,5]);
excelRange.Merge(excelRange.MergeCells);
excelRange1.Merge(excelRange1.MergeCells);
excelRange4.Merge(excelRange4.MergeCells);
excelRange2.Merge(excelRange2.MergeCells);
excelRange3.Merge(excelRange3.MergeCells);
excelRange5.Merge(excelRange5.MergeCells);
excelRange6.Merge(excelRange6.MergeCells);
Microsoft.Office.Interop.Excel.Range columnRange = sheet.get_Range("A1", "H2"); //得到 Range 范圍 A-H 表示1-8列,1-2表示跨幾行
columnRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
columnRange.Font.Size = 10;
columnRange.Font.Bold = true;
#endregion
#region 3.添加行數(shù)據(jù),直接給Range賦值可提高效率
Microsoft.Office.Interop.Excel.Range range = sheet.get_Range("A3", "H" + (dgv_Result.Rows.Count + 2).ToString()); //得到 Range 范圍
string[,] AryData = new string[dgv_Result.Rows.Count-1, dgv_Result.Columns.Count];
for (int i = 0; i < dgv_Result.Rows.Count - 1; i++)
{
for (int j = 0; j < dgv_Result.Columns.Count; j++)
{
AryData[i,j] = dgv_Result.Rows[i].Cells[j].Value.ToString();
}
}
range.Value2 = AryData;
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
#endregion
sheet.Cells.Columns.AutoFit();//設(shè)置Excel表格的 列寬
excel.SheetsInNewWorkbook = 1;//設(shè)置Excel單元格對(duì)齊方式
excel.DisplayAlerts = false; //設(shè)置禁止彈出保存和覆蓋的詢(xún)問(wèn)提示框
excel.AlertBeforeOverwriting = false;
//保存excel文件
xlBook.SaveAs(fielName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
MessageBox.Show("導(dǎo)出成功!", "提示");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "錯(cuò)誤提示");
}
finally
{
IntPtr pt = new IntPtr(excel.Hwnd);
int k = 0;
GetWindowThreadProcessId(pt, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
}
}
- C# DatagridView常用操作匯總
- c#獲取gridview的值代碼分享
- c#利用Excel直接讀取數(shù)據(jù)到DataGridView
- c#讀取xml文件到datagridview實(shí)例
- C# DataGridView添加新行的2個(gè)方法
- C#與SQL連接:GridView控件對(duì)數(shù)據(jù)庫(kù)的操作
- C#精髓 GridView72大絕技 學(xué)習(xí)gridview的朋友必看
- C#導(dǎo)入導(dǎo)出EXCEL文件的代碼實(shí)例
- c#生成excel示例sql數(shù)據(jù)庫(kù)導(dǎo)出excel
- C# WinForm導(dǎo)出Excel方法介紹
- C#使用RenderControl將GridView控件導(dǎo)出到EXCEL的方法
相關(guān)文章
ADO.NET實(shí)體數(shù)據(jù)模型詳細(xì)介紹
本文將詳細(xì)介紹ADO.NET實(shí)體數(shù)據(jù)模型,下面先看看簡(jiǎn)單的單表的增刪改查操作,然后再看多表的關(guān)聯(lián)查詢(xún),帶參數(shù)查詢(xún)等2012-11-11
C#中SQL參數(shù)傳入空值報(bào)錯(cuò)解決方案
這篇文章主要介紹了C#中SQL參數(shù)傳入空值報(bào)錯(cuò)解決方案,需要的朋友可以參考下2017-06-06
C#實(shí)現(xiàn)按數(shù)據(jù)庫(kù)郵件列表發(fā)送郵件的方法
這篇文章主要介紹了C#實(shí)現(xiàn)按數(shù)據(jù)庫(kù)郵件列表發(fā)送郵件的方法,涉及C#讀取數(shù)據(jù)庫(kù)及通過(guò)自定義函數(shù)發(fā)送郵件的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
.NET(C#):Emit創(chuàng)建異常處理的方法
.NET(C#):Emit創(chuàng)建異常處理的方法,需要的朋友可以參考一下2013-04-04
C#實(shí)現(xiàn)winform中RichTextBox在指定光標(biāo)位置插入圖片的方法
這篇文章主要介紹了C#實(shí)現(xiàn)winform中RichTextBox在指定光標(biāo)位置插入圖片的方法,涉及RichTextBox控件及剪切板的相關(guān)操作技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2016-06-06
C#實(shí)現(xiàn)控制線程池最大數(shù)并發(fā)線程
這篇文章主要介紹了C#實(shí)現(xiàn)控制線程池最大數(shù)并發(fā)線程的相關(guān)資料,需要的朋友可以參考下2016-07-07

