C# 将选定行datagridview导出到excel
我必须拿出一个程序将选定的行datagridview导出到excel,并带有相应的标题。我已经完成了将整个datagridview导出到excel的工作,但现在我只想使用选定的行。我怎么做 以下是我将整个datagridview导出到excel的代码 私有void GenerateExcel(数据表表3,字符串名称) {C# 将选定行datagridview导出到excel,c#,visual-studio-2010,excel,datagridview,export-to-excel,C#,Visual Studio 2010,Excel,Datagridview,Export To Excel,我必须拿出一个程序将选定的行datagridview导出到excel,并带有相应的标题。我已经完成了将整个datagridview导出到excel的工作,但现在我只想使用选定的行。我怎么做 以下是我将整个datagridview导出到excel的代码 私有void GenerateExcel(数据表表3,字符串名称) { string fileName = "TestingofDSI"; //string currentDirectorypath = "C:\te
string fileName = "TestingofDSI";
//string currentDirectorypath = "C:\testingdsi";
string currentDirectorypath = Environment.CurrentDirectory;
string finalFileNameWithPath = string.Empty;
fileName = string.Format("{0}_{1}", fileName, DateTime.Now.ToString("dd-MM-yyyy"));
finalFileNameWithPath = string.Format("{0}\\{1}.xlsx", currentDirectorypath, fileName);
var newFile = new FileInfo(finalFileNameWithPath);
using (var package = new ExcelPackage(newFile))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(excelSheetName);
if (table != null)
{
worksheet.Cells["A1"].LoadFromDataTable(table3, true, OfficeOpenXml.Table.TableStyles.Medium2);
//worksheet.Cells["A1"].LoadFromDataTable(table, true, TableStyles.None);
/* package.Workbook.Properties.Title = @"This code is part of tutorials available at http://bytesofcode.hubpages.com";
package.Workbook.Properties.Author = "Bytes Of Code";
package.Workbook.Properties.Subject = @"Register here for more http://hubpages.com/_bytes/user/new/";*/
package.Save();
MessageBox.Show(string.Format("File name '{0}' generated successfully.", fileName)
, "File generated successfully!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("please load data from database!");
}
}
}
//绑定数据的代码
private void loadButton_Click(object sender, EventArgs e)
{
GetData(" select * from jacksonpc.product;");
dataGridView1.DataSource = bindingSource1;
}
private void GetData(string selectCommand)
{
try
{
// Specify a connection string. Replace the given value with a
// valid connection string for a Northwind SQL Server sample
// database accessible to your system.
String connectionString = "datasource=localhost;port=3306;username=root;password=1234";
// Create a new data adapter based on the specified query.
dataAdapter = new MySqlDataAdapter(selectCommand, connectionString);
// Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(dataAdapter);
// Populate a new data table and bind it to the BindingSource.
table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
/* table2 = new DataTable();
table2.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table2);
bindingSource2.DataSource = table2;
// Resize the DataGridView columns to fit the newly loaded content.*/
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
您可以更改构造函数中的
datagridview
SelectionMode
属性或Form\u Load
dataGridView1.SelectionMode=System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
然后,用户将能够在
CTRL
或Shift
的帮助下选择多个行
在“打印”方法上,必须创建一个包含选定行的数据表
:
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
//Create a DataTable with same column name
//fill each column like row.Cells[1].Value.ToString();
}
您是否添加了任何复选框来选择行?@Arshad我必须这样做吗?是否有其他替代方法?那么,如果用户想要选择多行,您将如何识别所选行您想要使用单个行吗row@Arshad我可以通过点击其中一个按钮来识别,它会突出显示整行。我想用h不止一行。这将如何将所选行导出到excel?我可以选择多行,但无法将它们提取到excel。首先,您必须创建一个datatable,然后创建新行,将新行添加到datatable…然后调用print()通过传递新创建的DataTable感谢您的帮助,我现在可以这样做了,但我现在有了新问题…我可以通过您的电子邮件或其他方式向您发送屏幕截图吗?您只能在此处讨论。请在问题it self中附上详细信息