我有一個WinForm
,這是爲了過濾和顯示我的數據庫中的數據到它的DataGridView
。爲了過濾,我放置了一個combobox
,其中顯示了數據庫中列的名稱並且可以選擇該名稱,並且可以輸入textbox
,用戶可以在其中輸入關鍵字或短語。然後用戶點擊Filter
按鈕執行過濾。如何將過濾的數據從SQL Server導出到Excel?
我也有一個按鈕來導出數據庫表到Excel文件。
我的濾波方法:
protected void searchFilter()
{
DataTable dt;
BindingSource bs = new BindingSource();
_db.conn();
_db.cmd.CommandText = "SELECT * FROM IncomingLog";
dt = _db.executeDT();
DataView dv = new DataView(dt);
incomLogTableS.DataSource = dv;
String cmbCat = cmbFilterIDLS.GetItemText(cmbFilterIDLS.SelectedValue.ToString());
String keyID = keyIDLS.Text;
if (cmbCat != "Select Category")
{
if (cmbCat == "Received Date")
{
dv.RowFilter = string.Format("[Date Received] LIKE '%{0}%'", keyID);
}
else if (cmbCat == "Reference Number")
{
dv.RowFilter = string.Format("[Reference Number] LIKE '%{0}%'", keyID);
}
else if (cmbCat == "Received Time")
{
dv.RowFilter = string.Format("[Time Received] LIKE '%{0}%'", keyID);
}
else if (cmbCat == "Title/Description")
{
dv.RowFilter = string.Format("[Title/Description] LIKE '%{0}%'", keyID);
}
else if (cmbCat == "Originating Office")
{
dv.RowFilter = string.Format("[Originating Office] LIKE '%{0}%'", keyID);
}
else if (cmbCat == "Received By")
{
dv.RowFilter = string.Format("[Receiving Person] LIKE '%{0}%'", keyID);
}
else if (cmbCat == "Filed Under")
{
dv.RowFilter = string.Format("[Filed Under] LIKE '%{0}%'", keyID);
}
else
{
dv.RowFilter = string.Format("[Encoded By] LIKE '%{0}%'", keyID);
}
}
else
{
MessageBox.Show("Please select a category to search!", "Select category", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
if (dv.Count == 0)
{
MessageBox.Show("No records found! \nPlease try with different keyword(s).", "(0) Records Found", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
的出口按鈕的方法:
private void exportIDLS_Click(object sender, EventArgs e)
{
searchFilter();
string data = null;
int i = 0;
int j = 0;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
{
xlWorkSheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
range = xlWorkSheet.Cells[1, j + 1];
range.Interior.ColorIndex = 15;
range.Font.Bold = true;
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
xlWorkSheet.Cells[i + 2, j + 1] = data;
}
}
Microsoft.Office.Interop.Excel.Range columns = xlWorkSheet.UsedRange.Columns;
columns.AutoFit();
xlApp.StandardFont = "Arial";
xlApp.StandardFontSize = 11;
xlWorkSheet.Rows[1].Insert();
Excel.Range newRow = xlWorkSheet.Rows[1];
Excel.Range newCell = newRow.Cells[1];
newCell.Value = "Incoming Documents Log Summary";
newCell.Font.Size = 12;
newCell.Font.Bold = true;
xlWorkBook.SaveAs("Incoming Documents Log Summary.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Success! Incoming Documents Log Summary.xls is created! \nPlease look at the Documents folder.", "Excel file created!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
這是現在的工作,但是導出Excel文件得到所有從數據庫中的數據。我能做些什麼,因此Export
按鈕從已過濾的DataGridView
獲取數據?
請幫幫我。非常感謝你!
嗨!就像你說的那樣,我嘗試了ToTable()方法,並且我得到了一個空白的Excel文件。我把它放在'Export'按鈕的方法上。我還爲DataView創建了一個新實例。這裏有什麼問題? – ohmokipo
此外,還有一個「過濾器」按鈕,它使用searchFilter方法來進行過濾。我在那裏放置ToTable()方法嗎?謝謝! – ohmokipo
請參閱我的編輯瞭解更多。 –