谁那有ASP。NET关与EXECL操作的代码,50分求!!!
数据导入Execl问题谁解决过,有代码的请帖上来。。。。 --------------------编程问答-------------------- 在VB.NET同样可以将报表导出到Excel和Word进行输出,制作出专业水平的报表。具体操作如下:(注:首先需添加引用,选择COM-->选择Microsoft Word Object Library和Microsoft Excel Object Library组件)Private Function CreaTable() As DataTable
Dim dt As New DataTable()
dt.Columns.Add("列1", GetType(String))
dt.Columns.Add("列2", GetType(Integer))
dt.Columns.Add("列3", GetType(String))
dt.Columns.Add("列4", GetType(String))
Dim row, row1 As DataRow
row = dt.NewRow()
row!列1 = "行1"
row!列2 = 1
row!列3 = "d"
row!列4 = "a"
dt.Rows.Add(row)
row1 = dt.NewRow()
row1!列1 = "行2"
row1!列2 = 12
row1!列3 = "b"
row1!列4 = "c"
dt.Rows.Add(row1)
Return dt
End Function
'2.将表中的内容导出到Excel
Dim xlApp As New Excel.Application()
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim rowIndex As Integer = 1
Dim colIndex As Integer = 0
xlBook = xlApp.Workbooks().Add
xlSheet = xlBook.Worksheets("sheet1")
Dim Table As New DataTable()
Table = CreaTable()
'将所得到的表的列名,赋值给单元格
Dim Col As DataColumn
Dim Row As DataRow
For Each Col In Table.Columns
colIndex = colIndex + 1
xlApp.Cells(1, colIndex) = Col.ColumnName
Next
'得到的表所有行,赋值给单元格
For Each Row In Table.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each Col In Table.Columns
colIndex = colIndex + 1
xlApp.Cells(rowIndex, colIndex) = Row(Col.ColumnName)
Next
Next
With xlSheet
.Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Name = "黑体"
'设标题为黑体字
.Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Bold = True
'标题字体加粗
.Range(.Cells(1, 1), .Cells(rowIndex, colIndex)).Borders.LineStyle = 1
'设表格边框样式
End With
With xlSheet.PageSetup
.LeftHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10公司名称:" ' & Gsmc
.CenterHeader = "&""楷体_GB2312,常规""公司人员情况表&""宋体,常规""" & Chr(10) &_
"&""楷体_GB2312,常规""&10日 期:"
.RightHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10单位:"
.LeftFooter = "&""楷体_GB2312,常规""&10制表人:"
.CenterFooter = "&""楷体_GB2312,常规""&10制表日期:"
.RightFooter = "&""楷体_GB2312,常规""&10第&P页 共&N页"
End With
xlApp.Visible = True
'3.将表中的内容导出到WORD
Dim wordApp As New Word.Application()
Dim myDoc As Word.Document
Dim oTable As Word.Table
Dim rowIndex, colIndex As Integer
rowIndex = 1
colIndex = 0
wordApp.Documents.Add()
myDoc = wordApp.ActiveDocument
Dim Table As New DataTable()
Table = CreaTable()
oTable = myDoc.Tables.Add(Range:=myDoc.Range(Start:=0, End:=0), _
NumRows:=Table.Rows.Count + 1, NumColumns:=Table.Columns.Count)
'将所得到的表的列名,赋值给单元格
Dim Col As DataColumn
Dim Row As DataRow
For Each Col In Table.Columns
colIndex = colIndex + 1
oTable.Cell(1, colIndex).Range.InsertAfter(Col.ColumnName)
Next
'得到的表所有行,赋值给单元格
For Each Row In Table.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each Col In Table.Columns
colIndex = colIndex + 1
oTable.Cell(rowIndex, colIndex).Range.InsertAfter(Row(Col.ColumnName))
Next
Next
oTable.Borders.InsideLineStyle = 1
oTable.Borders.OutsideLineStyle = 1
wordApp.Visible = True
--------------------编程问答-------------------- private void btnReadData_Click(object sender, System.EventArgs e)
{
if(File1.Value.Length>1)
{
string fileName = System.IO.Path.GetFileName(File1.PostedFile.FileName);
string filePath = "";
if(this.File1.Value == "")
{
// Comm.Jscript.Alert("请先选择您要导入的文件!");
}
else
{
int index = fileName.LastIndexOf(".");
if(index > 0)
{
if(fileName.Substring(index) == ".xls")
{
DateTime now = DateTime.Now;
fileName = now.ToShortDateString() + now.ToLongTimeString();
fileName = fileName.Replace("-","").Replace(":","").Replace(" ","");
filePath = @"../uploads/" + fileName + ".xls";
this.File1.PostedFile.SaveAs(Server.MapPath(filePath));
}
else
{
//Comm.Jscript.Alert("读入的文件不是XLS");
}
}
}
if(filePath != "")
{
fileName = Request.MapPath(filePath);
// string strConn ="Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + Dir + "\\"+fileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1;\"
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ fileName +";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
string query = "SELECT * FROM [Sheet1$]";
OleDbCommand oleCommand = new OleDbCommand(query, new OleDbConnection(strConn));
OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);
DataSet myDataSet = new DataSet();
// 将 Excel 的[Sheet1]表内容填充到 DataSet 对象
try
{
oleAdapter.Fill(myDataSet,"[Sheet1$]");
// 数据绑定
this.dgExportProject.DataSource = myDataSet;
this.dgExportProject.DataMember = "[Sheet1$]";
this.dgExportProject.DataBind();
this.dgExportProject.Visible = true;
this.btnSave.Visible = true;
}
catch(Exception exx)
{
Response.Write(exx.Message);
//Comm.Jscript.Alert("注意:请用默认的Sheet1$页名称!");
}
finally
{
if(File.Exists(filePath))
{
File.Delete(filePath);
}
}
}
}
else
{
Response.Write("<script>alert('请选择正确的路径!')</script>");
}
}
private void btnSave_Click(object sender, System.EventArgs e)
{
string conn=ConfigurationSettings.AppSettings["datasource"];
SqlConnection sqlConn = new SqlConnection(conn);
{
try
{
if(sqlConn.State == ConnectionState.Closed)
{
sqlConn.Open();
}
foreach(DataGridItem item in this.dgExportProject.Items)
{
if(item.Cells.Count == 2)
{
string wldm= item.Cells[0].Text.Trim().Replace(" "," ");
string cgjg=item.Cells[1].Text.Trim().Replace(" "," ");
string sSQL = "Insert Into wljg (wldm,cgjg) Values ('"+wldm +"','" + cgjg+ "')";
SqlCommand cmd = new SqlCommand(sSQL,sqlConn);
cmd.CommandType = CommandType.Text;
try
{
cmd.ExecuteNonQuery();
Response.Write("<script>alert('导入数据成功!')</script>");
}
catch
{
Response.Write("<script>alert('导入数据出错,请联系管理员!')</script>");
}
cmd.Dispose();
}
else
{
//Comm.Jscript.Alert("注意:请按照模板格式导入项目信息!");
return;
}
}
}
catch(Exception exx)
{
Response.Write(exx.Message);
}
finally
{
if(sqlConn.State == ConnectionState.Open)
{
sqlConn.Close();
}
}
}
this.dgExportProject.Visible = false;
this.btnSave.Visible = false;
}
}
--------------------编程问答-------------------- 你要是操作Excel的API就比较麻烦了
用OLEDB连上比较容易的 --------------------编程问答-------------------- StringWriter sw = new StringWriter();
sw.WriteLine("编号,状态,操作量");
for (int i = 0; i < 数据List.Count; i++)
{
sw.WriteLine(数据List[i].编号 + "','" +数据List[i].状态 + "','" +数据List[i].操作量);
}
sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=" + 文件名 + ".xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();
其中的“数据List”为泛型数据源。 --------------------编程问答-------------------- --------------------编程问答-------------------- 参考:
http://blog.csdn.net/insus/archive/2008/03/31/2231945.aspx --------------------编程问答-------------------- 本论坛就有,
补充:.NET技术 , ASP.NET