首先要添加EXCEl引用在网上搜一下应该就能找到添加的方法,注意要跟服务器中的EXCEl版本一直然后在cs中添加using Microsoft.Office.Interop.Excel;using Microsoft.Office.Core;using System.Reflection;using System.IO;可能还有别的,自己看VS提示加吧然后在Web.config中下加入计算机登陆的用户名密码,这是给予程序权限调用EXCEL DLL下面就是方法了添加一个DataTable就能导出EXCEl了注意网页编码 中文乱码问题 这个就自己解决吧哈哈,下面这个是我项目中用到的一个小例子 自己看着改吧
[csharp]
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="dt">要导出的DataTable</param>
public void ExportToExcel(System.Data.DataTable dt)
{
if (dt == null) return;
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
// lblMsg.Text = "无法创建Excel对象,可能您的机子未安装Excel";
lblMsg.Text = GetLocalResourceObject("noexcel").ToString();
return;
}
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
Microsoft.Office.Interop.Excel.Range range = null;
long totalCount = dt.Rows.Count;
long rowRead = 0;
float percent = 0;
//表头
range = worksheet.get_Range("A1", "H1");
range.Merge(Missing.Value);
range.Font.Bold = true;
range.Font.Size = 16;
range.Value2 = "金润天公司原材料入库(报验)单";
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
//第2行
worksheet.Cells[2, 1] = "供货方:"; //Excel单元格赋值
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[2, 1];
range.Font.Bold = true;
range.Font.Size = 10;
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;
range = worksheet.get_Range("B2", "D2");
range.Merge(Missing.Value);
range.Font.Size = 10;
range.Value2 = DropDownList2.Text;
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
range.EntireColumn.AutoFit();
worksheet.Cells[2, 5] = "日期:"; //Excel单元格赋值
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[2, 5];
range.Font.Bold = true;
range.Font.Size = 10;
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
worksheet.Cells[2, 6] = add_timetb.Text; //Excel单元格赋值
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[2, 6];
range.Font.Size = 10;
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
range.EntireColumn.AutoFit();
worksheet.Cells[2, 7] = "单号:"; //Excel单元格赋值
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[2,7];
range.Font.Bold = true;
range.Font.Size = 10;
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
worksheet.Cells[2, 8] = ins_idtb.Text; //Excel单元格赋值
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[2, 8];
range.Font.Size = 10;
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
range.EntireColumn.AutoFit();
//第3行
worksheet.Cells[3, 1] = "合同号:"; //Excel单元格赋值
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[3, 1];
range.Font.Bold = true;
range.Font.Size = 10;
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;
range = worksheet.get_Range("B3", "D3");
range.Merge(Missing.Value);
range.Font.Size = 10;
range.Value2 = c_id_lb.Text;//Excel单元格赋值
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
range.EntireColumn.AutoFit();
//第4行
worksheet.Cells[4, 1] = "订单号:"; //Excel单元格赋值
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[4, 1];
range.Font.Bold = true;
range.Font.Size = 10;
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;
range = worksheet.get_Range("B4", "D4");
range.Merge(Missing.Value);
range.Font.Size = 10;
range.Value2 = dingdan_numtb.Text;//Excel单元格赋值
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
range.EntireColumn.AutoFit();
//第5行
worksheet.Cells[5, 1] = "入库明细:"; //Excel单元格赋值
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[5, 1];
range.Font.Bold = true;
range.Font.Size = 10;
range.Ho
补充:Web开发 , ASP.Net ,