C#下在vs2010中的导出数据到Excel中
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Core ;
using Excel=Microsoft.Office.Interop.Excel;
namespace ExcelReport
{
public partial class FormExcelNormal : Form
{
public FormExcelNormal()
{
InitializeComponent();
}
private void FormExcelNormal_Load(object sender, EventArgs e)
{
DataSet objDataSet = FormUtil.FormGridView("MEMBERINFO");
dgvMemberInfo.DataSource = objDataSet.Tables["MEMBERINFO"];
}
// Excel导出
private void btnExcelOut_Click(object sender, EventArgs e)
{
string strConnect = System.Configuration.ConfigurationSettings.AppSettings["connStr"];
SqlConnection objConnection = new SqlConnection(strConnect);
SqlCommand objCommand = new SqlCommand("SELECT MEMBERNAME, SEX, AREA, EMAIL FROM MEMBERINFO", objConnection);
DataSet objDataset = new DataSet();
SqlDataAdapter objAdapter = new SqlDataAdapter(objCommand);
// 数据填充
objConnection.Open();
objAdapter.Fill(objDataset, "Excel");
objConnection.Close();
// 创建Excel对象
Excel.Application xlApp = new Excel.Application();
Excel.Workbook wb = xlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
Excel.Range targetRange = xlApp.get_Range("A1");
// 设置标题
xlApp.Cells[1, 1] = "会员姓名";
xlApp.Cells[1, 2] = "性别";
xlApp.Cells[1, 3] = "籍贯";
xlApp.Cells[1, 4] = "电子邮件";
// 设置格式
int iMaxRow = objDataset.Tables["Excel"].Rows.Count;
int iMaxCol = objDataset.Tables["Excel"].Columns.Count;
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, iMaxCol]).Font.Name = "黑体";
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, iMaxCol]).Font.Bold = true;
ws.get_Range(ws.Cells[1, 1], ws.Cells[iMaxRow + 1, iMaxCol]).Borders.LineStyle = 1;
// 填充数据
for (int iRow = 0; iRow < iMaxRow; iRow++)
{
for (int iCol = 0; iCol < iMaxCol; iCol++)
{
xlApp.Cells[iRow + 2, iCol + 1] = objDataset.Tables["Excel"].Rows[iRow][iCol].ToString();
}
}
// 保存Excel
xlApp.Save("sheet1.xls");
// 打开Excel
xlApp.Visible = true;
}
}
}
总是出现object未包含“get_Rangede”的定义 各位高手帮帮看看 --------------------编程问答-------------------- 你的 Excel 有这个 Range 吗? --------------------编程问答-------------------- 里面没用到Range的 --------------------编程问答-------------------- using Microsoft.Office.Core;
using Excel;
Excel.Application myExcel=new Excel.Application();
/// <summary>
/// 读取指定单元格数据
/// </summary>
/// <param name="row">行序号</param>
/// <param name="column">列序号</param>
/// <returns>该格的数据</returns>
public string ReadData(int row, int column)
{
Excel.Range range = myExcel.get_Range(myExcel.Cells[row, column], myExcel.Cells[row, column]);
return range.Text.ToString();
} --------------------编程问答-------------------- using Excel=Microsoft.Office.Interop.Excel;
--------------------编程问答-------------------- using Excel=Microsoft.Office.Interop.Excel; 楼主你的是winform 有这个控件?
我想问 asp.net做 服务端定义模板 ,服务器要不要装office? --------------------编程问答-------------------- http://download.csdn.net/detail/jx_521/5819725
补充:.NET技术 , C#