61,825
社区成员




Dim myexcel As New Excel.Application
Dim strSql As String = "select * from mdl_rc_case"
Dim table As New DataTable
table = DB.QueryTable(strSql)'DB是数据库链接类的实例,QueryTable返回的是datatable
myexcel.Application.Workbooks.Add(True)
Dim i, j As Integer
If count_table_rows = 0 Then
Return
End If
'添加标题
For i = 0 To table.Columns.Count - 1
myexcel.Cells(1, i + 1) = table.Columns(i).Caption
Next
'填充数据
For i = 0 To table.Rows.Count - 1
For j = 0 To table.Columns.Count - 1
myexcel.Cells(i + 2, j + 1) = table.Rows(i)(j).ToString()
Next
Next
myexcel.Visible = True
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelBook.ActiveSheet;
excelApp.Visible = true;
int rowCount = testRecordDataGridView.Rows.Count - 1;
int columnCount = testRecordDataGridView.Columns.Count;
if (txtBoardNo.Text != "")
{
excelSheet.Name = txtBoardNo.Text;
}
else
{
excelSheet.Name = "所有记录";
}
int sheetCount = rowCount / 65535 + 1;
for (int i = 0; i < sheetCount; i++)
{
for (int j = 1; j <= testRecordDataGridView.Columns.Count; j++)
{
excelSheet.Cells[1, (i * 10) + j] = testRecordDataGridView.Columns[j - 1].HeaderText;
}
string[,] records = new string[65535, columnCount];
for (int row = i * 65535; row < (i + 1) * 65535; row++)
{
if (row < rowCount)
{
for (int col = 0; col < columnCount; col++)
{
records[row % 65535, col] = testRecordDataGridView[col, row].Value.ToString();
}
}
}
excelSheet.get_Range(excelSheet.Cells[2, i * 10 + 1], excelSheet.Cells[65536, i * 10 + 9]).Value2 = records;
}
}