Asp.net 数据的导入导出,数据的导入和导出在实际的应用中非常的普遍,本文介绍了数据导入和导出的一种方法。
数据的导入和导出在实际的项目中,用的非常普遍。我在自己的项目中实现了从excel文件中导入和到处数据,现分析如下:
一、数据的导出,其基本思想是首先将要导出的内容放在Table中并拼成字符串,然后以EXCEL文件的形式导出。在我的项目中,用户可以自己选择要导出的数据字段。因为实际的字段较多,故只列出部分字段。
页面的html如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<div id="DivProjectProperty" style="text-align: center; color: White">
<fieldset>
<legend>项目导出字段</legend>
<div id="DivProjectBasic" style="margin-top: 5px">
<fieldset style="text-align: left;">
<legend>项目基本字段</legend>
<table id="tableProjectBasic" cellpadding="0" cellspacing="0" width="99%" style="margin-left: 10px">
<tr>
<td>
<input type="checkbox" id="CheckboxProjectBasicSelectAll" name="ProjectBasic" class="ProjectBasic"
onclick="selectProjectPropertyAll(this)" />选择全部
</td>
<td>
<input type="checkbox" id="CheckboxProjectName" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />名称
</td>
<td>
<input type="checkbox" id="CheckboxProjectSerialNumber" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />流水号
</td>
<td>
<input type="checkbox" id="CheckboxProjectNumber" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />编号
</td>
<td>
<input type="checkbox" id="CheckboxProjectPrincipalNumber" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />负责人工作证号
</td>
<td>
<input type="checkbox" id="CheckboxProjectPrincipalName" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />负责人姓名
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="CheckboxProjectState" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />状态
</td>
<td>
<input type="checkbox" id="CheckboxProjectStartDate" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />开始日期
</td>
<td>
<input type="checkbox" id="CheckboxProjectEndDate" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />结束日期
</td>
<td>
<input type="checkbox" id="CheckboxProjectCreator" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />创立人
</td>
<td>
<input type="checkbox" id="CheckboxProjectCreateDate" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />创立日期
</td>
<td>
<input type="checkbox" id="CheckboxprojectUpdateDate" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />更新日期
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="CheckboxProjectSubjectCode" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />学科代码
</td>
<td>
<input type="checkbox" id="CheckboxProjectResearchType" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />研究类型
</td>
<td>
<input type="checkbox" id="CheckboxProjectIsSerect" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />是否保密
</td>
<td>
<input type="checkbox" id="CheckboxProjectBaseName" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />基地名称
</td>
<td>
<input type="checkbox" id="CheckboxProjectCooperationType" class="ProjectBasic" checked="checked"
runat="server" onclick="changeSelectAllState(this)" />合作类型
</td>
<td>
<input type="checkbox" id="CheckboxProjectUnit" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />合作单位
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="CheckboxPorjectTaskFrom" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />任务来源
</td>
<td>
<input type="checkbox" id="CheckboxProjectCorporationPlace" class="ProjectBasic"
checked="checked" runat="server" onclick="changeSelectAllState(this)" />公司地址
</td>
<td>
<input type="checkbox" id="CheckboxProjectRemark" class="ProjectBasic" runat="server"
checked="checked" onclick="changeSelectAllState(this)" />备注
</td>
</tr>
</table>
</fieldset>
</div>
</fieldset>
</div>
<div id="DivOperator" style="text-align: center; margin-top: 10px">
<asp:Button ID="ButtonExport" runat="server" Text="导出项目" Style="margin-right: 5px"
OnClick="ButtonExport_Click" /><input type="button" id="ButtonCancel" value="取 消"
style="width: 70px" onclick="history.go(-2)" />
</div>
后台代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
其中核心代码就是最后的那几条语句,在导出数据的时候不会刷新页面是这种方法的有点之一。
二、数据的导入
数据导入的基本思想就是,首先将EXCEL文件上传至服务器,然后读出数据放在Dataset中,最后删除这个EXCEL文件。代码如下:
首先是上传数据代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/// <summary>
/// 从EXCEL中获取数据(放入dataset中)
/// </summary>
/// <param name="page"></param>
/// <param name="fileUploadContent"></param>
/// <param name="filename">文件名</param>
/// <returns></returns>
public static DataSet GetDataFromUploadFile(this PageBase page, FileUpload fileUploadContent, string filename)
{
//将文件上传至服务器
fileUploadContent.PostedFile.SaveAs(page.Server.MapPath("fileupload\\") + filename);
string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + page.Server.MapPath("fileupload\\") + filename + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
string workSheetName = GetExcelWorkSheet(page.Server.MapPath("fileupload\\") + filename);
string sqlin = "SELECT * FROM [" + workSheetName + "$]";
OleDbCommand oleCommand = new OleDbCommand(sqlin, new OleDbConnection(conn));
OleDbDataAdapter adapterIn = new OleDbDataAdapter(oleCommand);
DataSet dsIn = new DataSet();
adapterIn.Fill(dsIn, "Finance");
return dsIn;
}
// 返回给定路径 Excel文件的workSheet名字
private static string GetExcelWorkSheet(string filePath)
{
Microsoft.Office.Interop.Excel.ApplicationClass myExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
string xlName;
//'得到worksheet名字
myExcel.Workbooks.Open(filePath, Missing.Value, false, Missing.Value, null, null, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Microsoft.Office.Interop.Excel.Sheets mySh = myExcel.Sheets;
Microsoft.Office.Interop.Excel.Worksheet myWs = (Microsoft.Office.Interop.Excel.Worksheet)mySh.get_Item(1);
xlName = myWs.Name;
//'关闭excel处理
myExcel.Workbooks.Close();
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(mySh);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myWs);
mySh = null;
myWs = null;
myExcel = null;
GC.Collect();
//'返回名字字符串
return xlName;
}
/// <summary>
/// 删除上传的文件
/// </summary>
/// <param name="page"></param>
/// <param name="filename"></param>
public static void DeleteFile(this PageBase page, string filename)
{
FileInfo file = new FileInfo(page.Server.MapPath("fileupload/") + filename);
if (file.Exists)
file.Delete();
}
然后是页面对这个方法的调用
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
protected void ButtonUploadOK_Click(object sender, EventArgs e)
{
//取得上除的文件名
string filename = FileUploadContent.PostedFile.FileName.Substring(FileUploadContent.PostedFile.FileName.LastIndexOf("\\"));
DataSet dateSet = this.GetDataFromUploadFile(FileUploadContent, filename);
foreach (DataRow row in dateSet.Tables["Finance"].Rows)
{
Finance finance = new Finance();
string dateString = row[0].ToString().Substring(0, 4) + "/" + row[0].ToString().Substring(4, 2) + "/" + row[0].ToString().Substring(6, 2);
finance.ReceivedDate = Convert.ToDateTime(dateString);
finance.VoucherNumber = row[1].ToString();
finance.Description = row[2].ToString();
finance.Amount = Convert.ToInt64(Convert.ToDecimal(row[3].ToString()) * 100);
finance.Save(Database);
}
this.DeleteFile(filename);
Display();
}
这样就实现了将Excel文件中的数据导入到数据库中,代码都经过验证。