如何检查要上传的格式良好的Excel文件 [英] How to check that well formated excel file going to upload
本文介绍了如何检查要上传的格式良好的Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的朋友们,我有一张excel表,其中有一栏,
i我将excel文件保存到我的数据库中。
i am validating文件扩展名如.xls,.xlsx
但我想知道用户选择的文件是否具有名称为DATA的表格
如果工作表存在而不是我要检查
excel工作表中的列与要求的格式?
Dear friends i have an excel sheet in which some column,
i am saving that excel file into my database.
i am validating file extension like .xls, .xlsx
but i want to know that the file which user select have the sheet with name "DATA" or not
if the sheet exist than i want to check
that the column which are there in the excel sheet is same like the required format?
推荐答案
http://www.aspdotnet-suresh.com/2012/12/how-to-import-data-from-excel-to-aspnet.html [<一个href =http://www.aspdotnet-suresh.com/2012/12/how-to-import-data-from-excel-to-aspnet.html\"target =_ blanktitle =新窗口> ^ ]
cheack it
http://www.aspdotnet-suresh.com/2012/12/how-to-import-data-from-excel-to-aspnet.html[^]
cheack it
<form enctype="multipart/form-data">
<input type="file" id="excel-file" name="excel-file" />
<input type="submit" />
</form>
protected void Page_Load(object sender, EventArgs e)
{
if(this.IsPostback)
{
var yourFile = this.Request.Files["excel-file"];
}
}
您必须使用以下逻辑步骤附加文件一致性检查:
1.打开创建的excel文件
2.检查是否存在具有特定名称的表格。
如果存在,则:
2.1检查列(更准确地说是单元格),了解它们的数据类型。
以下是您可以使用的示例:
You will have to append your file consistency check with the following logicas steps:
1. Open the created excel file
2. Check if sheet, with the specific name, exists.
if it exists, then:
2.1 Check the columns (more precise it would be to say cells) for what data type they are.
Here is an example you can use:
private void Excel_Inspector(string Path_to_your_selected_file)
{
try{
//create excel application object
Microsoft.Office.Interop.Excel.Application Excel_App = new Microsoft.Office.Interop.Excel.Application();
//create excel workbook object and open the excel file for inspection
Microsoft.Office.Interop.Excel.Workbook Excel_Wbk = Excel_App.Workbooks.Open(Path_to_your_selected_file);
//this will make excel visible (you can set it to false if you like)
Excel_App.Visible = true;
//this will count how many sheets your excel has
int sheet_number = Excel_Wbk.Sheets.Count;
//loop through all excel sheets reading how the sheets are named
while (sheet_number >= 1)
{
//check if sheet name is "DATA"
if (Excel_Wbk.Sheets[sheet_number].Name == "DATA")
{
/* create string var and assign the value to it - in this case I am reading
* cells "A1" number format. Please note that if you have date value it will
* NOT say "short date" or "long date". instead it will show you the actual format.
* For example "yyyy/dd/M"
*/
string Number_Format = Excel_App.Range["A1"].NumberFormat.ToString();
//do something with number format (for now - we're just displaying it)
MessageBox.Show(Number_Format);
}
sheet_number--;
}
/* closing and cleanup part */
//close and don't save the file (true = save, false = don't)
Excel_Wbk.Close(false);
Excel_App.Workbooks.Close();
Excel_App.Quit();
//release objects
System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel_Wbk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel_App);
}catch (Exception ex)
{MessageBox.Show(ex.Message); }
希望这会让你走上正轨。
祝你好运!
Modestas
Hope this puts you on the track.
Good luck!
Modestas
这篇关于如何检查要上传的格式良好的Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文