参考:http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.saveas(VS.80).aspx
-------------------------------------------------------------------
/// <summary>
/// 导出数据到excel
/// </summary>
/// <param name="mFileName">文件名</param>
private void ExportDataToExcel(string mFileName)
{
// 没有数据
if (ds == null)
{
return;
}
// 获得当前系统中EXCEL进程
System.Diagnostics.Process[] localByName = System.Diagnostics.Process.GetProcessesByName("EXCEL");
// 声明保存进程ID的数组
int[] I_ProcessId = new int[localByName.Length];
// 保存当前系统中的EXCEL进程ID
for (int i = 0; i < localByName.Length; i++)
{
I_ProcessId[i] = localByName[i].Id;
}
// 打开模版文件
// string fileNamePath = find file path
System.IO.FileInfo fi = new System.IO.FileInfo(fileNamePath);
//如果模版不存在
if (!fi.Exists)
{
//alert("模版不存在")
return;
}
// 声明Excel变量
Excel.ApplicationClass xlsApp = new Excel.ApplicationClass();
Excel.Workbook xlsBook = xlsApp.Workbooks.Add(fi.FullName);
//多个sheet
Excel.Worksheet xlsSheet1 = (Excel.Worksheet)xlsBook.Sheets.get_Item("shee1");
//also: Excel.Worksheet xlsSheetTotal = (Excel.Worksheet)xlsBook.Sheets.get_Item(1);
Excel.Worksheet xlsSheet2 = (Excel.Worksheet)xlsBook.Sheets.get_Item("sheet2");
//输出数据
DataTable dt = ds.Tables[0];
int length = dt.Rows.Count;
//初始行
int k = 1;
int rowindex = 1;//行索引
foreach (DataRow dr in dt.Rows)
{
xlsSheetTotal.Cells[k, 1] = rowindex;
xlsSheetTotal.Cells[k, 2] = dr["po_no"].ToString();
xlsSheetTotal.Cells[k, 3] = dr["coustomer_no"].ToString();
xlsSheetTotal.Cells[k, 4] = dr["date"].ToString();
k++;
rowindex++;
}
// winform保存对话框
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "excel files (*.xls)|*.xls|All files (*.*)|*.*";
saveFileDialog.FilterIndex = 1;
saveFileDialog.ShowDialog();
string fileName = saveFileDialog.FileName;
// 保存EXCEL(保存多个sheet用xlsbook对象,one sheet use xlssheet object)
object o = System.Reflection.Missing.Value;
xlsBook.SaveAs(fileName, o, o, o, o, o, Excel.XlSaveAsAccessMode.xlNoChange, o, o, o, o);
bool Is_OldProcess = false;
// 获得系统中的EXCEL进程
localByName = System.Diagnostics.Process.GetProcessesByName("EXCEL");
// 找出新生成的EXCEL进程,并杀死
for (int i = 0; i < localByName.Length; i++)
{
// 循环保存进程ID
for (int j = 0; j < I_ProcessId.Length; j++)
{
// 判断是否与打印前保存的EXCEL进程ID相等
if (localByName[i].Id == I_ProcessId[j])
{
Is_OldProcess = true;
break;
}// end if ( localByName[i].Id == I_ProcessId[j] )
}
// 如果不是打印前的EXCEL进程则杀死
if (!Is_OldProcess)
{
localByName[i].Kill();
Is_OldProcess = false;
}
}// end for ( int i=0 ;i<localByName.Length;i++)
/*
// 这种方式会关掉已经打开的所有excel )
foreach (System.Diagnostics.Process myProcess in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
{
if (!myProcess.CloseMainWindow())//winform mode (可以弹出一个对话框提示用户是否保存所有已打开的excel)
{
myProcess.Kill();
}
}
*/
}
---------------------------------------------------------------------
单元格的合并、求和、颜色设置等(Excel.Range对象)
-----------------------------------------------------------------------
Excel单元格的合并
Excel.Application myExcel=new Excel.Application();
myExcel.Application.Workbooks.Add(true);
myExcel.Visible=true;
Excel.Worksheet worksheet=(Excel.Worksheet)myExcel.ActiveSheet;
Excel.Range rH = worksheet.get_Range("A1","A3");
rH.Merge(0);
myExcel.Cells[1,1]="赋值";
---------------------------------------------------------------------------
C#操作Excel时,Excel常用的方法和属性zz
生成excel的时候有时候需要设置单元格的一些属性,可以参考一下:
range.NumberFormatLocal = "@"; //设置单元格格式为文本
range = (Range)worksheet.get_Range("A1", "E1"); //获取Excel多个单元格区域:本例做为Excel
表头
range.Merge(0); //单元格合并动作
worksheet.Cells[1, 1] = "Excel单元格赋值"; //Excel单元格赋值
range.Font.Size = 15; //设置字体大小
range.Font.Underline=true; //设置字体是否有下划线
range.Font.Name="黑体"; 设置字体的种类
range.HorizontalAlignment=XlHAlign.xlHAlignCenter; //设置字体在单元格内的对其方式
range.ColumnWidth=15; //设置单元格的宽度
range.Cells.Interior.Color=System.Drawing.Color.FromArgb(255,204,153).ToArgb(); //设置单
元格的背景色
range.Borders.LineStyle=1; //设置单元格边框的粗细
range.BorderAround
(XlLineStyle.xlContinuous,XlBorderWeight.xlThick,XlColorIndex.xlColorIndexAutomatic,System.D
rawing.Color.Black.ToArgb()); //给单元格加边框
range.EntireColumn.AutoFit(); //自动调整列宽
Range.HorizontalAlignment= xlCenter; // 文本水平居中方式
Range.VerticalAlignment= xlCenter //文本垂直居中方式
Range.WrapText=true; //文本自动换行
Range.Interior.ColorIndex=39; //填充颜色为淡紫色
Range.Font.Color=clBlue; //字体颜色
xlsApp.DisplayAlerts=false; //保存Excel的时候,不弹出是否保存的窗口直接进行保存
workbook.SaveCopyAs(temp);/**填入完信息之后另存到路径及文件名字
---------------------------------------------------------
//设定要存放求和结果的单元格
range = objSheet.get_Range("A5" , "A5");
//对该单元格的上面四行求和
range.FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)";
----------------------------------------------------------------
使用如下VBA代码取得相应的单元格底色
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/1327ab569c1ae82736693a50b8e33378.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0196c3df5ea9e936f21e9932cca91014.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
结果如下
无色 | 0 | 19 | 38 | ||
1 | 20 | 39 | |||
白色 | 2 | 21 | 40 | ||
3 | 22 | 41 | |||
4 | 23 | 42 | |||
5 | 24 | 43 | |||
6 | 25 | 44 | |||
7 | 26 | 45 | |||
8 | 27 | 46 | |||
9 | 28 | 47 | |||
10 | 29 | 48 | |||
11 | 30 | 49 | |||
12 | 31 | 50 | |||
13 | 32 | 51 | |||
14 | 33 | 52 | |||
15 | 34 | 53 | |||
16 | 35 | 54 | |||
17 | 36 | 55 | |||
18 | 37 | 56 |