VB。NET 和C# 基本上是差不多的 简单的修改一下应该能实现的! Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim mydataset As New DataSet Dim myadapter As OleDbDataAdapter Dim myconnection As New OleDbConnection Dim str As String = Nothing Dim Table As New System.Data.DataTable myconnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + "\1dbTT.mdb" myconnection.Open() str = "select * from tab_DATA_Send" myadapter = New OleDbDataAdapter(str, myconnection) myadapter.Fill(mydataset, "queueDateTime") Table = mydataset.Tables("queueDateTime") Dim total_col As Integer = Table.Columns.Count Dim total_row As Integer = Table.Rows.Count Dim xlApp As New Excel.Application Dim xlBook As Excel.Workbook = Nothing Dim xlSheet As Excel.Worksheet Try If total_col < 1 Or total_row < 1 Then MsgBox("没有可供导入的数据!", MsgBoxStyle.Information, "系统提示") Exit Sub End If GC.Collect() xlBook = xlApp.Workbooks().Add xlSheet = xlBook.Worksheets("sheet1") xlApp.Visible = True Dim Col As Integer Dim Row As Integer Dim st_row As Integer = 1 ''数据列头开始行,(列头) Dim trueCol As Integer = Table.Columns.Count Dim HeaderArray(0, trueCol) As Object '定义一个存放表的各个字段的数组 Dim DataArray(total_row - 1, trueCol) As Object '定义一个存放表中除字段以外的内容的数组 For Col = 0 To total_col - 1 HeaderArray(0, Col) = Table.Columns(Col).ColumnName Next xlSheet.Range("A" & st_row).Resize(st_row, trueCol).Value = HeaderArray '存放到EXCEL中间从第一行开始 For Row = 0 To total_row - 1 For Col = 0 To total_col - 1 DataArray(Row, Col) = Table.Rows(Row).Item(Col) Next Next xlSheet.Range("A" & st_row + 1).Resize(total_row, trueCol).Value = DataArray Catch ex As Exception xlSheet = Nothing xlApp.DisplayAlerts = False xlBook.RunAutoMacros(Excel.XlRunAutoMacro.xlAutoClose) xlBook.Close() xlBook = Nothing xlApp.Quit() xlApp.DisplayAlerts = True xlApp = Nothing GC.Collect() MsgBox(ex.ToString) Exit Sub Finally xlSheet = Nothing xlApp.DisplayAlerts = False xlBook.RunAutoMacros(Excel.XlRunAutoMacro.xlAutoClose) '' xlBook.Close() '退出EXCEL 小界面 xlBook = Nothing ' 'xlApp.Quit() ‘退出EXCEL 窗体 xlApp.DisplayAlerts = True xlApp = Nothing GC.Collect() End Try End Sub
我不懂VB,请问 GC.Collect()是什么啊?没看到定义啊!
Excel.Application excel1=new Excel.Application(); excel1.Application.Workbooks.Add(true); int index=0; foreach(DataColumn col in dt.Columns) { index++; excel1.Cells[1,index]=col.ColumnName; } int rowindex=1; foreach(DataRow dr in dt.Rows) { rowindex++; index=0; foreach(DataColumn co in dt.Columns) { index++; excel1.Cells[rowindex,index]=dr[co.ColumnName].ToString(); } } excel1.Visible=true; }
If you are just exporting simple table structure, just save the dataset as a comma (,) separated file with extension .csv. Excel can directly open csv files.
简单的可以用Reporting Service或水晶报表
Dim myadapter As OleDbDataAdapter
Dim myconnection As New OleDbConnection
Dim str As String = Nothing
Dim Table As New System.Data.DataTable myconnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + "\1dbTT.mdb"
myconnection.Open()
str = "select * from tab_DATA_Send"
myadapter = New OleDbDataAdapter(str, myconnection)
myadapter.Fill(mydataset, "queueDateTime")
Table = mydataset.Tables("queueDateTime") Dim total_col As Integer = Table.Columns.Count
Dim total_row As Integer = Table.Rows.Count
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook = Nothing
Dim xlSheet As Excel.Worksheet Try If total_col < 1 Or total_row < 1 Then
MsgBox("没有可供导入的数据!", MsgBoxStyle.Information, "系统提示")
Exit Sub
End If
GC.Collect()
xlBook = xlApp.Workbooks().Add
xlSheet = xlBook.Worksheets("sheet1")
xlApp.Visible = True Dim Col As Integer
Dim Row As Integer
Dim st_row As Integer = 1 ''数据列头开始行,(列头)
Dim trueCol As Integer = Table.Columns.Count
Dim HeaderArray(0, trueCol) As Object '定义一个存放表的各个字段的数组
Dim DataArray(total_row - 1, trueCol) As Object '定义一个存放表中除字段以外的内容的数组 For Col = 0 To total_col - 1
HeaderArray(0, Col) = Table.Columns(Col).ColumnName
Next
xlSheet.Range("A" & st_row).Resize(st_row, trueCol).Value = HeaderArray '存放到EXCEL中间从第一行开始
For Row = 0 To total_row - 1
For Col = 0 To total_col - 1
DataArray(Row, Col) = Table.Rows(Row).Item(Col)
Next
Next
xlSheet.Range("A" & st_row + 1).Resize(total_row, trueCol).Value = DataArray
Catch ex As Exception
xlSheet = Nothing
xlApp.DisplayAlerts = False
xlBook.RunAutoMacros(Excel.XlRunAutoMacro.xlAutoClose)
xlBook.Close()
xlBook = Nothing
xlApp.Quit()
xlApp.DisplayAlerts = True
xlApp = Nothing
GC.Collect()
MsgBox(ex.ToString)
Exit Sub
Finally
xlSheet = Nothing
xlApp.DisplayAlerts = False
xlBook.RunAutoMacros(Excel.XlRunAutoMacro.xlAutoClose)
'' xlBook.Close() '退出EXCEL 小界面
xlBook = Nothing
' 'xlApp.Quit() ‘退出EXCEL 窗体
xlApp.DisplayAlerts = True
xlApp = Nothing
GC.Collect()
End Try
End Sub
excel1.Application.Workbooks.Add(true);
int index=0;
foreach(DataColumn col in dt.Columns)
{
index++;
excel1.Cells[1,index]=col.ColumnName;
}
int rowindex=1;
foreach(DataRow dr in dt.Rows)
{
rowindex++;
index=0;
foreach(DataColumn co in dt.Columns)
{
index++;
excel1.Cells[rowindex,index]=dr[co.ColumnName].ToString();
}
}
excel1.Visible=true;
}
不明白你的意思!我是要将数据导出为Excel啊,也就是最后变成一个.xls文件
---------------------------------------------------------------- 添加引用-->COM-->Microsoft Excel 9.0 Object Library
Excel can directly open csv files.
{
ExportDataGrid("application/ms-excel", "供应商信息.xls"); //导到Excel
} private void ExportDataGrid(string FileType, string FileName) //导出
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); Response.AppendHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState =true;
StringWriter tw = new StringWriter();
HtmlTextWriter hw =new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End(); }
mingwj1980() 的方法可能我的DataGrid里面放了CheckBox的缘故,并不能输出.对于此方法,我看不懂! 错误提示"类型“DataGridLinkButton”的控件“dg_ghs__ctl1__ctl0”必须放在具有 runat=server 的窗体标记内。"很有意思的是刚好我要导出信息的也是如你代码中的一样,也是"供货商信息",我的只是自己做着练手而已!losky(清风sky)和chendazhi(不务正业)提到的方法呢好象是在服务端生成.xls文件,问题就是老是提示"拒绝访问",可我已经把相关的文件夹中的安全属性都改为Everyone写入了.莫非非用第三方控件不可啊?
//输出到Excel
public void ExportExcel(DataView dv)
{
SaveFileDialog saveFileDialog1=new SaveFileDialog();
saveFileDialog1.Filter = "Excel files (*.xls)|*.xls" ;
saveFileDialog1.FilterIndex = 0 ;
saveFileDialog1.RestoreDirectory = true ;
saveFileDialog1.CreatePrompt=true;
saveFileDialog1.Title="导出Excel文件到"; DateTime now=DateTime.Now;
saveFileDialog1.FileName=now.Year.ToString().PadLeft(2)
+now.Month.ToString().PadLeft(2,'0')
+now.Day.ToString().PadLeft(2,'0')+"_"
+now.Hour.ToString().PadLeft(2,'0')
+now.Minute.ToString().PadLeft(2,'0')
+now.Second.ToString().PadLeft(2,'0'); saveFileDialog1.ShowDialog(); Stream myStream;
myStream=saveFileDialog1.OpenFile();
StreamWriter sw=new StreamWriter(myStream,System.Text.Encoding.GetEncoding("gb2312"));
String str="";
//写标题
for(int i=0;i<dv.Table.Columns.Count;i++)
{
if(i>0)
{
str+="\t";
}
str+=dv.Table.Columns[i].ColumnName;
}
sw.WriteLine(str);
//写内容
for(int rowNo=0;rowNo<dv.Count;rowNo++)
{
String tempstr="";
for(int columnNo=0;columnNo<dv.Table.Columns.Count;columnNo++)
{
if(columnNo>0)
{
tempstr+="\t";
}
//tempstr+=dg.Rows[rowNo,columnNo].ToString();
tempstr+=dv.Table.Rows[rowNo][columnNo].ToString();
}
sw.WriteLine(tempstr);
}
sw.Close();
myStream.Close();
}
我的实现,VS.net2003下已经测试通过