📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace Excel操作
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private string ConnectionString = "Server=.;uid=sa;pwd=sa;Database=pubs";
private string SelectCommandString = "Select * From Titles";
private string TableName = "Titles";
private System.Windows.Forms.DataGrid dataGrid1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(292, 266);
this.dataGrid1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter( SelectCommandString, ConnectionString );
DataSet ds = new DataSet();
da.Fill( ds, TableName );
this.dataGrid1.DataSource = ds;
this.dataGrid1.DataMember = TableName;
// //建立excel文件
Excel.ApplicationClass excel = new Excel.ApplicationClass();
excel.Visible = false;
object objMissing = System.Reflection.Missing.Value;
Excel.Workbook NewBook = (Excel.Workbook)excel.Workbooks.Add( 1 );
Excel.Worksheet NewSheet = (Excel.Worksheet)NewBook.Worksheets[1];
NewSheet.Name = "Haha";
// ((Excel.Range)NewSheet.Cells[2, 3]).EntireRow.Insert( 0, 0 );
// NewSheet.Cells[2, 3] = "Test";
int RowIndex = 1;
int ColIndex = 0;
DataTable Table = ds.Tables[TableName];
foreach ( DataColumn Col in Table.Columns )
{
ColIndex++;
NewSheet.Cells[1,ColIndex] = Col.ColumnName;
}
foreach ( DataRow Row in Table.Rows )
{
RowIndex++;
ColIndex = 0;
foreach ( DataColumn Col in Table.Columns )
{
ColIndex++;
NewSheet.Cells[RowIndex, ColIndex] = Row[Col.ColumnName].ToString();
}
}
NewBook.SaveAs( "E:\\Text.xls", objMissing, objMissing, objMissing, objMissing,
objMissing, Excel.XlSaveAsAccessMode.xlShared, objMissing, objMissing, objMissing, objMissing, objMissing );
NewBook = null;
excel.Quit();
excel = null;
// excel.Visible = false;
// excel.DisplayAlerts = false;
//
// try
// {
// excel.Save( @"C:\ExcelTable.xls" );
// excel.Application.Workbooks.Close();
// excel.Application.Quit();
// excel.Quit();
// }
// catch ( System.Exception exp )
// {
// MessageBox.Show( exp.Message );
// }
//
// System.Runtime.InteropServices.Marshal.ReleaseComObject( excel );
// GC.Collect();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -