//读取数据库,并且导出到Excal private void btn_读取数据库_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MyCinema;Integrated Security=True"); SqlDataAdapter adapter = new SqlDataAdapter("select * from Movie", con); DataSet ds = new DataSet(); adapter.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; //数据库的数据导入Excal Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); app.Workbooks.Add(); app.SheetsInNewWorkbook = 1; Worksheet w1 = (Worksheet)app.ActiveWorkbook.Worksheets[1]; w1.Name = "First"; for (int i = 1; i < dataGridView1.Rows.Count; i++) { for (int j = 1; j < dataGridView1.Rows[i].Cells.Count; j++) { w1.Cells[i, j] = dataGridView1.Rows[i - 1].Cells[j - 1].Value.ToString(); } } //给Excal设置样式,range表示区域,有格式属性,r.NumberFormat = "yyyy-m-d";事件格式 Range r = app.get_Range(app.Cells[1, 1], app.Cells[17, 5]); r.Font.Color = Color.Green; r.Borders.LineStyle = XlLineStyle.xlContinuous; app.Visible = true; app.ActiveWorkbook.SaveAs(Environment.CurrentDirectory + "/test.xls", XlFileFormat.xlWorkbookNormal); }
其中这里是给设置了某个区域内的单元格的样式,更多的希望楼主去看看Range 的属性
主要样式设置 //给Excal设置样式,range表示区域,有格式属性,r.NumberFormat = "yyyy-m-d";事件格式 Range r = app.get_Range(app.Cells[1, 1], app.Cells[17, 5]); r.Font.Color = Color.Green; r.Borders.LineStyle = XlLineStyle.xlContinuous;
我range怎么点不出来,是要导入什么命名空间吗?
我range怎么点不出来,是要导入什么命名空间吗?using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.Office.Interop.Excel; using System.Data.SqlClient; using System.Data.OleDb;
我range怎么点不出来,是要导入什么命名空间吗?using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.Office.Interop.Excel; using System.Data.SqlClient; using System.Data.OleDb; Range r = app.get_Range(app.Cells[1, 1], app.Cells[17, 5]); 这个APP是什么啊?
我range怎么点不出来,是要导入什么命名空间吗?using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.Office.Interop.Excel; using System.Data.SqlClient; using System.Data.OleDb; Range r = app.get_Range(app.Cells[1, 1], app.Cells[17, 5]); 这个APP是什么啊?我range怎么点不出来,是要导入什么命名空间吗?using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.Office.Interop.Excel; using System.Data.SqlClient; using System.Data.OleDb; Range r = app.get_Range(app.Cells[1, 1], app.Cells[17, 5]); 这个APP是什么啊?Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();z这个类
private void btn_读取数据库_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MyCinema;Integrated Security=True");
SqlDataAdapter adapter = new SqlDataAdapter("select * from Movie", con);
DataSet ds = new DataSet();
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
//数据库的数据导入Excal
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
app.Workbooks.Add();
app.SheetsInNewWorkbook = 1;
Worksheet w1 = (Worksheet)app.ActiveWorkbook.Worksheets[1];
w1.Name = "First";
for (int i = 1; i < dataGridView1.Rows.Count; i++) {
for (int j = 1; j < dataGridView1.Rows[i].Cells.Count; j++)
{
w1.Cells[i, j] = dataGridView1.Rows[i - 1].Cells[j - 1].Value.ToString();
}
}
//给Excal设置样式,range表示区域,有格式属性,r.NumberFormat = "yyyy-m-d";事件格式
Range r = app.get_Range(app.Cells[1, 1], app.Cells[17, 5]);
r.Font.Color = Color.Green;
r.Borders.LineStyle = XlLineStyle.xlContinuous; app.Visible = true;
app.ActiveWorkbook.SaveAs(Environment.CurrentDirectory + "/test.xls", XlFileFormat.xlWorkbookNormal);
}
//给Excal设置样式,range表示区域,有格式属性,r.NumberFormat = "yyyy-m-d";事件格式
Range r = app.get_Range(app.Cells[1, 1], app.Cells[17, 5]);
r.Font.Color = Color.Green;
r.Borders.LineStyle = XlLineStyle.xlContinuous;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Data.SqlClient;
using System.Data.OleDb;
就是多了一步excel的设置步骤
CreateObject("Excel.Application")oledb 什么的,不可能
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Data.SqlClient;
using System.Data.OleDb;
Range r = app.get_Range(app.Cells[1, 1], app.Cells[17, 5]);
这个APP是什么啊?
worksheet.Cells.ImportDataTable(pdt, true, 0, 0, rows, pdt.Columns.Count); //定义表格的标题
worksheet.Cells.InsertRow(0);
Range range = worksheet.Cells.CreateRange(worksheet.Cells[0, 0].Name, worksheet.Cells[0, pdt.Columns.Count - 1].Name);
range.RowHeight = 20;
range.Name = "ExcelTitle";
range.Merge();
Style style = workbook.Styles[workbook.Styles.Add()];
style.Font.Name = "宋体";
style.Font.IsBold = true;
style.Font.Size = 14;
style.HorizontalAlignment = TextAlignmentType.Center;
range.Style = style;
range[0, 0].PutValue(arrTitle[0]);
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Data.SqlClient;
using System.Data.OleDb;
Range r = app.get_Range(app.Cells[1, 1], app.Cells[17, 5]);
这个APP是什么啊?我range怎么点不出来,是要导入什么命名空间吗?using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Data.SqlClient;
using System.Data.OleDb;
Range r = app.get_Range(app.Cells[1, 1], app.Cells[17, 5]);
这个APP是什么啊?Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();z这个类