chào mọi người. em mới nghiên cứu c#. em muốn export dữ liệu từ listview ra file excell kkhi ấn button thì phải làm thế nào ạ. em đang cần gấp.
private void Export2Excel()
{
try
{
//lvPDF is nothing but the listview control name
string[] st = new string[lvPDF.Columns.Count];
DirectoryInfo di = new DirectoryInfo(@"c:\PDFExtraction\");
if (di.Exists == false)
di.Create();
StreamWriter sw = new StreamWriter(@"c:\PDFExtraction\" + txtBookName.Text.Trim() + ".xls", false);
sw.AutoFlush = true;
for (int col = 0; col < lvPDF.Columns.Count; col++)
{
sw.Write("\t" + lvPDF.Columns[col].Text.ToString());
}
int rowIndex = 1;
int row = 0;
string st1 = "";
for (row = 0; row < lvPDF.Items.Count; row++)
{
if (rowIndex <= lvPDF.Items.Count)
rowIndex++;
st1 = "\n";
for (int col = 0; col < lvPDF.Columns.Count; col++)
{
st1 = st1 + "\t" + "'" + lvPDF.Items[row].SubItems[col].Text.ToString();
}
sw.WriteLine(st1);
}
sw.Close();
FileInfo fil = new FileInfo(@"c:\PDFExtraction\" + txtBookName.Text.Trim() + ".xls");
if (fil.Exists == true)
MessageBox.Show("Process Completed", "Export to Excel", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
}
}
private void ToExcel()
{
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = true;
Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Add(1);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1];
int i = 1;
int i2 = 1;
foreach (ListViewItem lvi in myList.Items)
{
i = 1;
foreach (ListViewItem.ListViewSubItem lvs in lvi.SubItems)
{
ws.Cells[i2, i] = lvs.Text;
i++;
}
i2++;
}
}
Bạn tham khảo code này nhé:
Mã:private void Export2Excel() { try { //lvPDF is nothing but the listview control name string[] st = new string[lvPDF.Columns.Count]; DirectoryInfo di = new DirectoryInfo(@"c:\PDFExtraction\"); if (di.Exists == false) di.Create(); StreamWriter sw = new StreamWriter(@"c:\PDFExtraction\" + txtBookName.Text.Trim() + ".xls", false); sw.AutoFlush = true; for (int col = 0; col < lvPDF.Columns.Count; col++) { sw.Write("\t" + lvPDF.Columns[col].Text.ToString()); } int rowIndex = 1; int row = 0; string st1 = ""; for (row = 0; row < lvPDF.Items.Count; row++) { if (rowIndex <= lvPDF.Items.Count) rowIndex++; st1 = "\n"; for (int col = 0; col < lvPDF.Columns.Count; col++) { st1 = st1 + "\t" + "'" + lvPDF.Items[row].SubItems[col].Text.ToString(); } sw.WriteLine(st1); } sw.Close(); FileInfo fil = new FileInfo(@"c:\PDFExtraction\" + txtBookName.Text.Trim() + ".xls"); if (fil.Exists == true) MessageBox.Show("Process Completed", "Export to Excel", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { } }
Hoặc:
Mã:private void ToExcel() { Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); app.Visible = true; Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Add(1); Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1]; int i = 1; int i2 = 1; foreach (ListViewItem lvi in myList.Items) { i = 1; foreach (ListViewItem.ListViewSubItem lvs in lvi.SubItems) { ws.Cells[i2, i] = lvs.Text; i++; } i2++; } }