<input type="file" name="excelfile" id="excelfile" style="float:left;margin:30 0px;" onchange="ExportToTable()"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.7/xlsx.core.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xls/0.7.4-a/xls.core.min.js"></script>
<script type="text/javascript">
<!--
function ExportToTable() {
var regex = /^([a-zA-Z0-9가-힣\s_\\.\-:])+(.xlsx|.xls)$/;
/*Checks whether the file is a valid excel file*/
if (regex.test($("#excelfile").val().toLowerCase())) {
var xlsxflag = false; /*Flag for checking whether excel is .xls format or .xlsx format*/
if ($("#excelfile").val().toLowerCase().indexOf(".xlsx") > 0) {
xlsxflag = true;
}
/*Checks whether the browser supports HTML5*/
if (typeof(FileReader) != "undefined") {
var reader = new FileReader();
reader.onload = function(e) {
var data = e.target.result;
/*Converts the excel data in to object*/
if (xlsxflag) {
var workbook = XLSX.read(data, {
type: 'binary'
});
} else {
var workbook = XLS.read(data, {
type: 'binary'
});
}
/*Gets all the sheetnames of excel in to a variable*/
var sheet_name_list = workbook.SheetNames;
var cnt = 0; /*This is used for restricting the script to consider only first sheet of excel*/
sheet_name_list.forEach(function(y) { /*Iterate through all sheets*/
/*Convert the cell value to Json*/
if (xlsxflag) {
var exceljson = XLSX.utils.sheet_to_json(workbook.Sheets[y]);
} else {
var exceljson = XLS.utils.sheet_to_row_object_array(workbook.Sheets[y]);
}
if (exceljson.length > 0 && cnt == 0) {
console.log(exceljson); //데이터를 가공하여 사용
cnt++;
}
});
}
if (xlsxflag) { /*If excel file is .xlsx extension than creates a Array Buffer from excel*/
reader.readAsArrayBuffer($("#excelfile")[0].files[0]);
} else {
reader.readAsBinaryString($("#excelfile")[0].files[0]);
}
} else {
alert("Sorry! Your browser does not support HTML5!");
}
} else {
alert("Please upload a valid Excel file!");
}
}
//-->
</script>
'Development > Javascript, jQuery' 카테고리의 다른 글
input text 정규식을 이용하여 숫자만 입력하게 하기 (0) | 2020.11.04 |
---|---|
[jQuery] 드래그 앤 드롭 이용하여 파일 첨부하기 (0) | 2018.10.10 |
익스플로러 referrer 체크 이슈 (0) | 2018.04.19 |
[javascript] 다음 도로명,주소 postcode api 사용하기 (0) | 2018.02.09 |
[jQuery] cycle 이용한 간단한 롤링 (0) | 2018.01.19 |