导出excel小结(C#,.NET,Wpf)

时间:2022-06-13 01:54:53
range.NumberFormatLocal = "@";     //设置单元格格式为文本     
  range.NumberFormatLocal = "@";     //设置单元格格式为文本     

 range = (Range)worksheet.get_Range("A1", "E1");     //获取Excel多个单元格区域:本例做为Excel表头     

 range.Merge();     //单元格合并动作     

 worksheet.Cells[, ] = "Excel单元格赋值";     //Excel单元格赋值     

 range.Font.Size = ;     //设置字体大小     

 range.Font.Underline=true;     //设置字体是否有下划线     

 range.Font.Name="黑体";       设置字体的种类     

 range.HorizontalAlignment=XlHAlign.xlHAlignCenter;     //设置字体在单元格内的对其方式     

 range.ColumnWidth=;     //设置单元格的宽度     

 range.Cells.Interior.Color=System.Drawing.Color.FromArgb(,,).ToArgb();     //设置单元格的背景色     

 range.Borders.LineStyle=;     //设置单元格边框的粗细     

 range.BorderAround(XlLineStyle.xlContinuous,XlBorderWeight.xlThick,XlColorIndex.xlColorIndexAutomatic,System.Drawing.Color.Black.ToArgb());     //给单元格加边框     

 range.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone; //设置单元格上边框为无边框     

    range.EntireColumn.AutoFit();     //自动调整列宽     

 Range.HorizontalAlignment= xlCenter;     // 文本水平居中方式     

 Range.VerticalAlignment= xlCenter     //文本垂直居中方式     

 Range.WrapText=true;     //文本自动换行     

 Range.Interior.ColorIndex=;     //填充颜色为淡紫色     

 Range.Font.Color=clBlue;     //字体颜色     

 xlsApp.DisplayAlerts=false;     //保存Excel的时候,不弹出是否保存的窗口直接进行保存  

Excel 行高,列宽,合并单元格,单元格边框线,冻结

 private _Workbook _workBook = null;
private Worksheet _workSheet = null;
private Excel.Application _excelApplicatin = null; _excelApplicatin = new Excel.Application();
_excelApplicatin.Visible = true;
_excelApplicatin.DisplayAlerts = true; _workBook = _excelApplicatin.Workbooks.Add(XlSheetType.xlWorksheet);
_workSheet = (Worksheet)_workBook.ActiveSheet;
_workSheet.Name = "workSheetName"; //打开已存在的Excel
string strExcelPathName = AppDomain.CurrentDomain.BaseDirectory + "excelSheetName.xls";
Excel.Workbook workBook = application.Workbooks.Open(strExcelPathName, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//读取已打开的Excel
Excel.Worksheet workSheet1 = (Excel.Worksheet)workBook.Sheets["SheetName1"];
Excel.Worksheet workSheet2 = (Excel.Worksheet)workBook.Sheets["SheetName2"]; //添加一个workSheet
Worksheet workSheet = (Worksheet)workBook.Worksheets.Add(System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing); //RowHeight "1:1"表示第一行, "1:2"表示,第一行和第二行
((Excel.Range)_workSheet.Rows["1:1", System.Type.Missing]).RowHeight = ; //ColumnWidth "A:B"表示第一列和第二列, "A:A"表示第一列
((Excel.Range)_workSheet.Columns["A:B", System.Type.Missing]).ColumnWidth = ; // EXCEL操作(需要冻结的字段 按住ALT+W 再按F)
Excel.Range excelRange = _workSheet .get_Range(_workSheet .Cells[, ], _workSheet .Cells[, ]);
excelRange.Select();
excelApplication.ActiveWindow.FreezePanes = true; //Borders.LineStyle 单元格边框线
Excel.Range excelRange = _workSheet.get_Range(_workSheet.Cells[, ], _workSheet.Cells[, ]);
//单元格边框线类型(线型,虚线型)
excelRange.Borders.LineStyle = ;
excelRange.Borders.get_Item(XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlLineStyle.xlContinuous;
//指定单元格下边框线粗细,和色彩
excelRange.Borders.get_Item(XlBordersIndex.xlEdgeBottom).Weight = Excel.XlBorderWeight.xlMedium; excelRange.Borders.get_Item(XlBordersIndex.xlEdgeBottom).ColorIndex =; //设置字体大小
excelRange.Font.Size = ;
//设置字体是否有下划线
excelRange.Font.Underline = true; //设置字体在单元格内的对其方式
excelRange.HorizontalAlignment = XlHAlign.xlHAlignCenter;
//设置单元格的宽度
excelRange.ColumnWidth = ;
//设置单元格的背景色
excelRange.Cells.Interior.Color = System.Drawing.Color.FromArgb(, , ).ToArgb();
// 给单元格加边框
excelRange.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThick,
XlColorIndex.xlColorIndexAutomatic, System.Drawing.Color.Black.ToArgb());
//自动调整列宽
excelRange.EntireColumn.AutoFit();
// 文本水平居中方式
excelRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
//文本自动换行
excelRange.WrapText = true;
//填充颜色为淡紫色
excelRange.Interior.ColorIndex = ; //合并单元格
excelRange.Merge(excelRange.MergeCells);
_workSheet.get_Range("A15", "B15").Merge(_workSheet.get_Range("A15", "B15").MergeCells);

在Excel中设置背景字体及其它颜色的时候用的都是索引

如: xSt.get_Range(excel.Cells[2, i], excel.Cells[2, i]).Font.ColorIndex = 5

特此提供一个对应的Excel颜色索引图例

interior font HTML bgcolor= Red< Green Blue Color
Black [Color 1] #000000 #000000 0 0 0 [Black]
White [Color 2] #FFFFFF #FFFFFF 255 255 255 [White]
Red [Color 3] #FF0000 #FF0000 255 0 0 [Red]
Green [Color 4] #00FF00 #00FF00 0 255 0 [Green]
Blue [Color 5] #0000FF #0000FF 0 0 255 [Blue]
Yellow [Color 6] #FFFF00 #FFFF00 255 255 0 [Yellow]
Magenta [Color 7] #FF00FF #FF00FF 255 0 255 [Magenta]
Cyan [Color 8] #00FFFF #00FFFF 0 255 255 [Cyan]
[Color 9] [Color 9] #800000 #800000 128 0 0 [Color 9]
[Color 10] [Color 10] #008000 #008000 0 128 0 [Color 10]
[Color 11] [Color 11] #000080 #000080 0 0 128 [Color 11]
[Color 12] [Color 12] #808000 #808000 128 128 0 [Color 12]
[Color 13] [Color 13] #800080 #800080 128 0 128 [Color 13]
[Color 14] [Color 14] #008080 #008080 0 128 128 [Color 14]
[Color 15] [Color 15] #C0C0C0 #C0C0C0 192 192 192 [Color 15]
[Color 16] [Color 16] #808080 #808080 128 128 128 [Color 16]
[Color 17] [Color 17] #9999FF #9999FF 153 153 255 [Color 17]
[Color 18] [Color 18] #993366 #993366 153 51 102 [Color 18]
[Color 19] [Color 19] #FFFFCC #FFFFCC 255 255 204 [Color 19]
[Color 20] [Color 20] #CCFFFF #CCFFFF 204 255 255 [Color 20]
[Color 21] [Color 21] #660066 #660066 102 0 102 [Color 21]
[Color 22] [Color 22] #FF8080 #FF8080 255 128 128 [Color 22]
[Color 23] [Color 23] #0066CC #0066CC 0 102 204 [Color 23]
[Color 24] [Color 24] #CCCCFF #CCCCFF 204 204 255 [Color 24]
[Color 25] [Color 25] #000080 #000080 0 0 128 [Color 25]
[Color 26] [Color 26] #FF00FF #FF00FF 255 0 255 [Color 26]
[Color 27] [Color 27] #FFFF00 #FFFF00 255 255 0 [Color 27]
[Color 28] [Color 28] #00FFFF #00FFFF 0 255 255 [Color 28]
[Color 29] [Color 29] #800080 #800080 128 0 128 [Color 29]
[Color 30] [Color 30] #800000 #800000 128 0 0 [Color 30]
[Color 31] [Color 31] #008080 #008080 0 128 128 [Color 31]
[Color 32] [Color 32] #0000FF #0000FF 0 0 255 [Color 32]
[Color 33] [Color 33] #00CCFF #00CCFF 0 204 255 [Color 33]
[Color 34] [Color 34] #CCFFFF #CCFFFF 204 255 255 [Color 34]
[Color 35] [Color 35] #CCFFCC #CCFFCC 204 255 204 [Color 35]
[Color 36] [Color 36] #FFFF99 #FFFF99 255 255 153 [Color 36]
[Color 37] [Color 37] #99CCFF #99CCFF 153 204 255 [Color 37]
[Color 38] [Color 38] #FF99CC #FF99CC 255 153 204 [Color 38]
[Color 39] [Color 39] #CC99FF #CC99FF 204 153 255 [Color 39]
[Color 40] [Color 40] #FFCC99 #FFCC99 255 204 153 [Color 40]
[Color 41] [Color 41] #3366FF #3366FF 51 102 255 [Color 41]
[Color 42] [Color 42] #33CCCC #33CCCC 51 204 204 [Color 42]
[Color 43] [Color 43] #99CC00 #99CC00 153 204 0 [Color 43]
[Color 44] [Color 44] #FFCC00 #FFCC00 255 204 0 [Color 44]
[Color 45] [Color 45] #FF9900 #FF9900 255 153 0 [Color 45]
[Color 46] [Color 46] #FF6600 #FF6600 255 102 0 [Color 46]
[Color 47] [Color 47] #666699 #666699 102 102 153 [Color 47]
[Color 48] [Color 48] #969696 #969696 150 150 150 [Color 48]
[Color 49] [Color 49] #003366 #003366 0 51 102 [Color 49]
[Color 50] [Color 50] #339966 #339966 51 153 102 [Color 50]
[Color 51] [Color 51] #003300 #003300 0 51 0 [Color 51]
[Color 52] [Color 52] #333300 #333300 51 51 0 [Color 52]
[Color 53] [Color 53] #993300 #993300 153 51 0 [Color 53]
[Color 54] [Color 54] #993366 #993366 153 51 102 [Color 54]
[Color 55] [Color 55] #333399 #333399 51 51 153 [Color 55]
[Color 56] [Color 56] #333333 #333333 51 51 51 [Color 56]

在Excel中设置背景字体及其它颜色的时候用的都是索引

如: xSt.get_Range(excel.Cells[2, i], excel.Cells[2, i]).Font.ColorIndex = 5

特此提供一个对应的Excel颜色索引图例

还有就是可能是大家最关心的Excel.exe的问题了,分享我们项目中的几个释放进程的方法,希望对大家有用,都是绝对好使的

 #region 释放excel.exe  

         [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); public void KillSpecialExcel()
{
try
{
if (excel != null)
{
int lpdwProcessId;
GetWindowThreadProcessId(new IntPtr(excel.Hwnd), out lpdwProcessId); System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();
}
}
catch (Exception ex)
{
Console.WriteLine("Delete Excel Process Error:" + ex.Message);
}
}
#endregion
 #region KillAllExcel
public bool KillAllExcel()
{
try
{
if (m_objExcel != null)
{
m_objExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
//释放COM组件,其实就是将其引用计数减1
//System.Diagnostics.Process theProc;
foreach (System.Diagnostics.Process theProc in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
{
//先关闭图形窗口。如果关闭失败...有的时候在状态里看不到图形窗口的excel了,
//但是在进程里仍然有EXCEL.EXE的进程存在,那么就需要杀掉它
if (theProc.CloseMainWindow() == false)
{
theProc.Kill();
}
}
m_objExcel = null;
return true;
}
}
catch
{
return false;
}
return true;
}
#endregion