发布网友 发布时间:2024-10-24 12:54
共3个回答
热心网友 时间:2024-10-30 12:12
可以再加一个窗体提供选择要导出的字段 点击该窗体的Button才真正导出数据代码与楼上相似
这是选择字段的那个窗体的代码,好好研究一下:
public partial class SelectToPDFOrExcel : Form
{
public SelectToPDFOrExcel(DataGridView dgv)
{
InitializeComponent();
Dgv = dgv;
listInt = new List<int>();
}
private DataGridView Dgv;
public List<int> listInt { get; set; }
private void SelectToPDFOrExcel_Load(object sender, EventArgs e)
{
try
{
if (Dgv.Columns[0].HeaderText == "操作")
{
for (int i = 1; i < Dgv.Columns.Count; i++)
{
listTwo.Items.Add(Dgv.Columns[i].HeaderText);
}
}
else
{
for (int i = 0; i < Dgv.Columns.Count; i++)
{
listTwo.Items.Add(Dgv.Columns[i].HeaderText);
}
}
}
catch (Exception ex)
{
return;
}
}
private void btnRemove_Click(object sender, EventArgs e)
{
try
{
string selectvalue = listTwo.Text;
if (listTwo.Text == "")
{
MessageBox.Show("选择需要移除的项!","提示");
return;
}
listTwo.Items.RemoveAt(listTwo.SelectedIndex);
listOne.Items.Add(selectvalue);
}
catch (Exception ex)
{
return;
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
string selectvalue = listOne.Text;
if (listOne.Text == "")
{
MessageBox.Show("选择需要添加的项!","提示");
return;
}
listOne.Items.RemoveAt(listOne.SelectedIndex);
listTwo.Items.Add(selectvalue);
}
catch (Exception ex)
{
return;
}
}
private void btnOut_Click(object sender, EventArgs e)
{
try
{
if (listTwo.Items == null || listTwo.Items.Count == 0)
{
MessageBox.Show("请选择需要导出的字段!","提示");
return;
}
for (int i = 0; i < Dgv.Columns.Count; i++)
{
for (int j = 0; j < listTwo.Items.Count; j++)
{
if (Dgv.Columns[i].HeaderText == listTwo.Items[j].ToString())
{
listInt.Add(i);
}
}
}
this.Close();
}
catch (Exception ex)
{
return;
}
}
private void SelectToPDFOrExcel_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyValue == 13)
{
if (listTwo.Items == null || listTwo.Items.Count == 0)
{
MessageBox.Show("请选择需要导出的字段!","提示");
return;
}
for (int i = 0; i < Dgv.Columns.Count; i++)
{
for (int j = 0; j < listTwo.Items.Count; j++)
{
if (Dgv.Columns[i].HeaderText == listTwo.Items[j].ToString())
{
listInt.Add(i);
}
}
}
this.Close();
}
}
catch (Exception ex)
{
return;
}
}
private void SelectToPDFOrExcel_FormClosed(object sender, FormClosedEventArgs e)
{
this.Close();
}
}
热心网友 时间:2024-10-30 12:12
public static void GetDataGridView(DataGridView pDgv)
{
#region Excel导入DataGridView
//打开一个文件选择框
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
System.Data.DataTable lvTable = new System.Data.DataTable();
string fileName = ofd.FileName.Trim();
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ";" + "Extended Properties='Excel 8.0; IMEX=1;'";
OleDbConnection con = new OleDbConnection(connStr);
con.Open();
string strsql = "select * from [sheet1$]"; //Excel的工作表名
OleDbDataAdapter dta = new OleDbDataAdapter(strsql, connStr);
DataSet ds = new DataSet();
dta.Fill(ds);
lvTable = ds.Tables[0].Clone(); //克隆表结构
string lvColumnsName = ds.Tables[0].Columns[ds.Tables[0].Columns.Count - 1].ColumnName;
lvTable.Columns.RemoveAt(lvTable.Columns.Count - 1);
DataColumn lvColumn = new DataColumn(lvColumnsName,typeof(string));
lvTable.Columns.Add(lvColumn);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows[i][0].ToString().Trim() != "")
{
lvTable.Rows.Add(ds.Tables[0].Rows[i].ItemArray);
}
}
for (int i = 0; i < lvTable.Rows.Count; i++)
{
string a = (Convert.ToDouble(lvTable.Rows[i][lvTable.Columns.Count - 1]) * 100).ToString() + "%";
lvTable.Rows[i][lvTable.Columns.Count - 1] = a;
}
pDgv.DataSource = lvTable; //绑定
}
catch (Exception ex)
{
MessageBox.Show("文件打开失败" + ex.ToString());
}
}
#endregion
}
把datagridview传进去 不会百度HI我
热心网友 时间:2024-10-30 12:13
先设计一个需要弹出的页面 F2 ,然后再F2的load事件中做SQL查询和绑定datagridview的动作
然后再主窗体的button按钮的事件下放入如下代码
Form f2=new Form();
f2.Show();
热心网友 时间:2024-10-30 12:12
可以再加一个窗体提供选择要导出的字段 点击该窗体的Button才真正导出数据代码与楼上相似
这是选择字段的那个窗体的代码,好好研究一下:
public partial class SelectToPDFOrExcel : Form
{
public SelectToPDFOrExcel(DataGridView dgv)
{
InitializeComponent();
Dgv = dgv;
listInt = new List<int>();
}
private DataGridView Dgv;
public List<int> listInt { get; set; }
private void SelectToPDFOrExcel_Load(object sender, EventArgs e)
{
try
{
if (Dgv.Columns[0].HeaderText == "操作")
{
for (int i = 1; i < Dgv.Columns.Count; i++)
{
listTwo.Items.Add(Dgv.Columns[i].HeaderText);
}
}
else
{
for (int i = 0; i < Dgv.Columns.Count; i++)
{
listTwo.Items.Add(Dgv.Columns[i].HeaderText);
}
}
}
catch (Exception ex)
{
return;
}
}
private void btnRemove_Click(object sender, EventArgs e)
{
try
{
string selectvalue = listTwo.Text;
if (listTwo.Text == "")
{
MessageBox.Show("选择需要移除的项!","提示");
return;
}
listTwo.Items.RemoveAt(listTwo.SelectedIndex);
listOne.Items.Add(selectvalue);
}
catch (Exception ex)
{
return;
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
string selectvalue = listOne.Text;
if (listOne.Text == "")
{
MessageBox.Show("选择需要添加的项!","提示");
return;
}
listOne.Items.RemoveAt(listOne.SelectedIndex);
listTwo.Items.Add(selectvalue);
}
catch (Exception ex)
{
return;
}
}
private void btnOut_Click(object sender, EventArgs e)
{
try
{
if (listTwo.Items == null || listTwo.Items.Count == 0)
{
MessageBox.Show("请选择需要导出的字段!","提示");
return;
}
for (int i = 0; i < Dgv.Columns.Count; i++)
{
for (int j = 0; j < listTwo.Items.Count; j++)
{
if (Dgv.Columns[i].HeaderText == listTwo.Items[j].ToString())
{
listInt.Add(i);
}
}
}
this.Close();
}
catch (Exception ex)
{
return;
}
}
private void SelectToPDFOrExcel_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyValue == 13)
{
if (listTwo.Items == null || listTwo.Items.Count == 0)
{
MessageBox.Show("请选择需要导出的字段!","提示");
return;
}
for (int i = 0; i < Dgv.Columns.Count; i++)
{
for (int j = 0; j < listTwo.Items.Count; j++)
{
if (Dgv.Columns[i].HeaderText == listTwo.Items[j].ToString())
{
listInt.Add(i);
}
}
}
this.Close();
}
}
catch (Exception ex)
{
return;
}
}
private void SelectToPDFOrExcel_FormClosed(object sender, FormClosedEventArgs e)
{
this.Close();
}
}
热心网友 时间:2024-10-30 12:15
public static void GetDataGridView(DataGridView pDgv)
{
#region Excel导入DataGridView
//打开一个文件选择框
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
System.Data.DataTable lvTable = new System.Data.DataTable();
string fileName = ofd.FileName.Trim();
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ";" + "Extended Properties='Excel 8.0; IMEX=1;'";
OleDbConnection con = new OleDbConnection(connStr);
con.Open();
string strsql = "select * from [sheet1$]"; //Excel的工作表名
OleDbDataAdapter dta = new OleDbDataAdapter(strsql, connStr);
DataSet ds = new DataSet();
dta.Fill(ds);
lvTable = ds.Tables[0].Clone(); //克隆表结构
string lvColumnsName = ds.Tables[0].Columns[ds.Tables[0].Columns.Count - 1].ColumnName;
lvTable.Columns.RemoveAt(lvTable.Columns.Count - 1);
DataColumn lvColumn = new DataColumn(lvColumnsName,typeof(string));
lvTable.Columns.Add(lvColumn);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows[i][0].ToString().Trim() != "")
{
lvTable.Rows.Add(ds.Tables[0].Rows[i].ItemArray);
}
}
for (int i = 0; i < lvTable.Rows.Count; i++)
{
string a = (Convert.ToDouble(lvTable.Rows[i][lvTable.Columns.Count - 1]) * 100).ToString() + "%";
lvTable.Rows[i][lvTable.Columns.Count - 1] = a;
}
pDgv.DataSource = lvTable; //绑定
}
catch (Exception ex)
{
MessageBox.Show("文件打开失败" + ex.ToString());
}
}
#endregion
}
把datagridview传进去 不会百度HI我
热心网友 时间:2024-10-30 12:12
先设计一个需要弹出的页面 F2 ,然后再F2的load事件中做SQL查询和绑定datagridview的动作
然后再主窗体的button按钮的事件下放入如下代码
Form f2=new Form();
f2.Show();