C#使用winform簡單導(dǎo)出Excel的方法
本文實(shí)例講述了C#使用winform簡單導(dǎo)出Excel的方法。分享給大家供大家參考,具體如下:
using Excel;
在項目中引入Excel.dll
/// <summary>
/// 導(dǎo)出Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExportExcel_Click(object sender, EventArgs e)
{
DataTable dt = this.dgvWaterTicket.DataSource;
if (dt == null)
{
return;
}
if (dt.Rows.Count == 0)
{
return;
}
Excel.Application xlApp = new Excel.Application();
if (xlApp == null)
{
MessageBox.Show("請確保您的電腦已經(jīng)安裝Excel", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
xlApp.UserControl = true;
Excel.Workbooks workbooks = xlApp.Workbooks;
//根據(jù)模版產(chǎn)生新的workbook //Workbook workbook = workbooks.Add("D:\\aa.xls");
Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
if (worksheet == null)
{
MessageBox.Show("請確保您的電腦已經(jīng)安裝Excel", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
Excel.Range range;
long totalCount = dt.Rows.Count;
long rowRead = 0;
float percent = 0;
worksheet.Cells[1, 1] = frm.Text;//導(dǎo)出的標(biāo)題
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, dt.]).MergeCells = true; //合并單元格---列數(shù)
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 3]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//居中對齊
worksheet.get_Range(worksheet.Cells[1, 3], worksheet.Cells[1, 3]).ColumnWidth = 15; //列寬
worksheet.get_Range(worksheet.Cells[1, 2], worksheet.Cells[1, 2]).ColumnWidth = 15; //列寬
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 1]).ColumnWidth = 20; //列寬
//寫入字段
for (int i = 0; i < dt.Columns.Count; i++)
{
worksheet.Cells[2, i + 1] = dt.Columns[i].ColumnName;
range = (Excel.Range)worksheet.Cells[2, i + 1];
range.Interior.ColorIndex = 15;
range.Font.Bold = true;
}
//寫入數(shù)值
for (int r = 0; r < dt.Rows.Count; r++)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
worksheet.Cells[r + 3, i + 1] = dt.Rows[r][i];
}
rowRead++;
percent = ((float)(100 * rowRead)) / totalCount;
//System.Threading.Thread.Sleep(500);
//如果字的數(shù)量過多則自動換行。worksheet.Cells[r+1, 4]為worksheet.Cells[行, 列]
worksheet.get_Range(worksheet.Cells[r + 3, 4], worksheet.Cells[r + 1, 4]).Columns.WrapText = true; //自動換行
worksheet.get_Range(worksheet.Cells[r + 3, 4], worksheet.Cells[r + 3, 4]).Rows.AutoFit(); //自動加行高
//this.Text = "導(dǎo)出數(shù)據(jù)[" + percent.ToString("0.00") + "%]...";
}
range = worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[dt.Rows.Count + 2, dt.Columns.Count]);
range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
if (dt.Columns.Count > 1)
{
range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
}
xlApp.Visible = true;
}
catch
{
MessageBox.Show("到出Excel失?。?, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
//KillProcess("Excel");
GC.Collect();//強(qiáng)行銷毀
}
}
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#操作Excel技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》及《C#程序設(shè)計之線程使用技巧總結(jié)》
希望本文所述對大家C#程序設(shè)計有所幫助。
相關(guān)文章
C#讀取word中表格數(shù)據(jù)的方法實(shí)現(xiàn)
本文主要介紹了C#讀取word中表格數(shù)據(jù)的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06

