Javascript 如何从ASHX页面读取excel文件?
您好,我已经上传文件到一个亚马逊s3服务器,我如何读取excel文件,并希望发送excel数据到数据库。 我的代码是Javascript 如何从ASHX页面读取excel文件?,javascript,asp.net,filehandler,Javascript,Asp.net,Filehandler,您好,我已经上传文件到一个亚马逊s3服务器,我如何读取excel文件,并希望发送excel数据到数据库。 我的代码是 <script type="text/javascript"> var obj = null; $(function () { $('#fileupload').fileupload({ replaceFileInput: false, f
<script type="text/javascript">
var obj = null;
$(function () {
$('#fileupload').fileupload({
replaceFileInput: false,
formData: function (form) {
return [{ name: "name1", value: "value1" }, { name: "name2", value: "value2"}];
$('#btnGo').click(function () {
obj.submit();
});
});
</script>
var obj=null;
$(函数(){
$('#fileupload')。fileupload({
replaceFileInput:false,
formData:函数(表单){
返回[{name:“name1”,value:“value1”},{name:“name2”,value:“value2”}];
$('#btnGo')。单击(函数(){
obj.submit();
});
});
还有我的ashx页面,在那里我需要读取excel数据
public class AjaxFileHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var file = context.Request.Files[0];
string fileName=fileName = Guid.NewGuid().ToString() + file.FileName;
Stream streamContentFile = context.Request.Files[0].InputStream;
var iFileSize = context.Request.Files[0].ContentLength;
byte[] data = new byte[iFileSize];
int bytes_read = 0;
while (bytes_read < iFileSize)
{
int bytes_read_this_iteration = streamContentFile.Read(data, bytes_read, iFileSize - bytes_read);
streamContentFile.Close();
streamContentFile.Dispose();
CommonBLL.UploadTemporaryFilesToS3Bucket(fileName, data);
//Here i need to read excel code can you provide how to do that pleas
}
公共类AjaxFileHandler:IHttpHandler
{
公共void ProcessRequest(HttpContext上下文)
{
var file=context.Request.Files[0];
字符串fileName=fileName=Guid.NewGuid().ToString()+file.fileName;
streamContentFile=context.Request.Files[0]。InputStream;
var iFileSize=context.Request.Files[0].ContentLength;
字节[]数据=新字节[iFileSize];
int bytes_read=0;
while(字节\u读取
您需要两件事:
- 允许代码读取Excel内容的驱动程序
- 访问此文件
- 对excel数据的查询
- 我使用的驱动程序,必须安装在服务器上
- 该文件位于
文件夹中(在您的情况下,我想该文件应该放在公共库中)App\u Data
- 该查询是一个
查询;您可以使用公共DB代码段替换为UPDATE
或SELECT
查询INSERT
string fileName= Server.MapPath( "~/App_Data/MyFile.xls"); string sheetName= "Sheet1"; string connString = string.Format( "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=No'" , fileName); string command = string.Format("UPDATE [{0}${1}:{1}] SET F1='{2}'", sheetName,cellName, cellValue); using (OleDbConnection oledbConn = new OleDbConnection(connString)) { oledbConn.Open(); using (OleDbCommand cmd = new OleDbCommand(command, oledbConn)) cmd.ExecuteNonQuery(); oledbConn.Close(); }