public partial class Form1 : Form
{
OleDbConnection myConnection;
OleDbCommand myCommand;
OleDbDataReader myDataReader;
DataTable table;
string fileName = "";
public Form1()
{
InitializeComponent();
label2.Text = "";
if (this.checkBox1.Checked == false)
{
setCombobox();
}
else
{
this.cboCondition.Enabled = false;
}
}
public void setCombobox()
{
OleDbConnection conn;
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source=" + Application.StartupPath + @"/DB/moban.mdb";
conn = new OleDbConnection(strConnection);
try
{
conn.Open();
this.cboCondition.Items.Clear();
OleDbDataAdapter adp1 = new OleDbDataAdapter("select distinct name2 from xq", conn);
DataSet ds1 = new DataSet();
//adp1.Fill(ds1, "Products");
adp1.Fill(ds1);
this.cboCondition.DisplayMember = "name2";//(这是显示的)
this.cboCondition.ValueMember = "name2";//这是不显示的项就是可以看成数据表中的主键一样进行判断好用的(这是实际的)
this.cboCondition.DataSource = ds1.Tables[0];
}
catch (Exception e)
{
}
finally
{
conn.Close();
}
}
public void toExcel()
{
string sql;
string connstr = "Provider=Microsoft.Jet.OleDb.4.0;";
connstr += @"Data Source="+ Application.StartupPath + @"/DB/moban.mdb";
OleDbConnection cn = new OleDbConnection(connstr);
OleDbCommand cmd;
if (File.Exists(fileName + ".xls"))
{
File.Delete(fileName + ".xls");
}
try
{
cn.Open();
// TODO:计算Sheet数目,进行记录分段,将不同的数据段导入到不同的Sheet(Sheet数目不知道有没有限制:()
// TODO:文件名,Sheet名字的存在检测略
//每个Sheet只能最多保存65536条记录。
sql = @"select top 65535 * into [Excel 8.0;database=" + fileName + ".xls].[Sheet1] from xq";
if (this.cboCondition.Enabled == true && this.cboCondition.ValueMember.Trim() != string.Empty)
{
sql += " where name2 like '" + this.cboCondition.Text + "'";
}
cmd = new OleDbCommand(sql, cn);
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
this.label2.Text = "导出不成功!";
return;
}
finally
{
if(cn != null)
{
cn.Close();
cn.Dispose();
cn = null;
}
}
this.label2.Text = "导出成功!";
}
private void btnToExcel_Click(object sender, EventArgs e)
{
toExcel();
}
private void btnSaveFile_Click(object sender, EventArgs e)
{
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
fileName = this.saveFileDialog1.FileName;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox1.Checked == true)
{
this.cboCondition.Enabled = false;
}
else
{
this.cboCondition.Enabled = true;
}
}
#endregion
}
{
OleDbConnection myConnection;
OleDbCommand myCommand;
OleDbDataReader myDataReader;
DataTable table;
string fileName = "";
public Form1()
{
InitializeComponent();
label2.Text = "";
if (this.checkBox1.Checked == false)
{
setCombobox();
}
else
{
this.cboCondition.Enabled = false;
}
}
public void setCombobox()
{
OleDbConnection conn;
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source=" + Application.StartupPath + @"/DB/moban.mdb";
conn = new OleDbConnection(strConnection);
try
{
conn.Open();
this.cboCondition.Items.Clear();
OleDbDataAdapter adp1 = new OleDbDataAdapter("select distinct name2 from xq", conn);
DataSet ds1 = new DataSet();
//adp1.Fill(ds1, "Products");
adp1.Fill(ds1);
this.cboCondition.DisplayMember = "name2";//(这是显示的)
this.cboCondition.ValueMember = "name2";//这是不显示的项就是可以看成数据表中的主键一样进行判断好用的(这是实际的)
this.cboCondition.DataSource = ds1.Tables[0];
}
catch (Exception e)
{
}
finally
{
conn.Close();
}
}
public void toExcel()
{
string sql;
string connstr = "Provider=Microsoft.Jet.OleDb.4.0;";
connstr += @"Data Source="+ Application.StartupPath + @"/DB/moban.mdb";
OleDbConnection cn = new OleDbConnection(connstr);
OleDbCommand cmd;
if (File.Exists(fileName + ".xls"))
{
File.Delete(fileName + ".xls");
}
try
{
cn.Open();
// TODO:计算Sheet数目,进行记录分段,将不同的数据段导入到不同的Sheet(Sheet数目不知道有没有限制:()
// TODO:文件名,Sheet名字的存在检测略
//每个Sheet只能最多保存65536条记录。
sql = @"select top 65535 * into [Excel 8.0;database=" + fileName + ".xls].[Sheet1] from xq";
if (this.cboCondition.Enabled == true && this.cboCondition.ValueMember.Trim() != string.Empty)
{
sql += " where name2 like '" + this.cboCondition.Text + "'";
}
cmd = new OleDbCommand(sql, cn);
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
this.label2.Text = "导出不成功!";
return;
}
finally
{
if(cn != null)
{
cn.Close();
cn.Dispose();
cn = null;
}
}
this.label2.Text = "导出成功!";
}
private void btnToExcel_Click(object sender, EventArgs e)
{
toExcel();
}
private void btnSaveFile_Click(object sender, EventArgs e)
{
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
fileName = this.saveFileDialog1.FileName;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox1.Checked == true)
{
this.cboCondition.Enabled = false;
}
else
{
this.cboCondition.Enabled = true;
}
}
#endregion
}