168 lines
4.6 KiB
Plaintext
168 lines
4.6 KiB
Plaintext
<%@ WebHandler Language="C#" Class="importXlsx" %>
|
|
|
|
using System;
|
|
using System.Web;
|
|
using System.Runtime.Serialization.Json;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Web.Services.Protocols;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Web.SessionState;
|
|
using System.Data;
|
|
using System.Collections.Generic;
|
|
using NPOI;
|
|
using NPOI.HPSF;
|
|
using NPOI.HSSF;
|
|
using NPOI.HSSF.UserModel;
|
|
using NPOI.XSSF;
|
|
using NPOI.XSSF.UserModel;
|
|
using NPOI.POIFS;
|
|
using NPOI.Util;
|
|
using System.IO;
|
|
|
|
public class importXlsx : IHttpHandler {
|
|
|
|
authPermission objAuth = new authPermission();
|
|
|
|
public void ProcessRequest (HttpContext context) {
|
|
string xlsxFileCode = globalClass.CreateRandomCode(12);
|
|
string xlsxFileName = xlsxFileCode + ".xlsx";
|
|
string projPath = "uploads/xlsx/";
|
|
|
|
|
|
|
|
result objRet = new result();
|
|
DataContractJsonSerializer json = new DataContractJsonSerializer(objRet.GetType());
|
|
context.Response.ContentType = "application/json;charset=utf-8";
|
|
|
|
|
|
|
|
if (objAuth.isLogin() == false) {
|
|
objRet.ret = "no";
|
|
objRet.err_code = "9999";
|
|
objRet.message = "尚未登入!";
|
|
json.WriteObject(context.Response.OutputStream, objRet);
|
|
return;
|
|
}
|
|
|
|
string base64_string = (context.Request["base64"] == null) ? "" : context.Request["base64"].ToString();
|
|
|
|
if (base64_string == "") {
|
|
objRet.ret = "no";
|
|
objRet.err_code = "0003";
|
|
objRet.message = "無base64檔案!";
|
|
json.WriteObject(context.Response.OutputStream, objRet);
|
|
return;
|
|
}
|
|
|
|
string filePath = string.Format("BackEnd/uploads/xlsx/{0}.xlsx", xlsxFileCode);
|
|
|
|
string path = System.Web.HttpContext.Current.Server.MapPath("~/");
|
|
|
|
globalClass.Base64ToFile(base64_string, path + filePath);
|
|
|
|
//globalClass.Base64ToAzure(base64_string, projPath + xlsxFileName);
|
|
|
|
XSSFWorkbook workbook = new XSSFWorkbook(globalClass.Base64ToStream(base64_string));
|
|
|
|
XSSFSheet sheet = (XSSFSheet)workbook.GetSheetAt(0);
|
|
DataTable dt = new DataTable(xlsxFileCode);
|
|
|
|
XSSFRow headerRow = (XSSFRow)sheet.GetRow(0);
|
|
|
|
int asciiA = 1;
|
|
foreach (XSSFCell headerCell in headerRow) {
|
|
dt.Columns.Add("A" + asciiA.ToString());
|
|
objRet.columns.Add(headerCell.ToString());
|
|
col newCol = new col();
|
|
newCol.data = "A" + asciiA.ToString();
|
|
newCol.visible = "true";
|
|
newCol.title = headerCell.ToString();
|
|
objRet.cols.Add(newCol);
|
|
asciiA++;
|
|
}
|
|
|
|
int rowIndex = 0;
|
|
int rowNum = 0;
|
|
int py = sheet.PhysicalNumberOfRows;
|
|
|
|
foreach (XSSFRow row in sheet) {
|
|
|
|
DataRow dataRow = dt.NewRow();
|
|
|
|
string hasData = "N";
|
|
|
|
foreach (XSSFCell cell in row.Cells) {
|
|
if (cell.ToString() != "")
|
|
{
|
|
hasData = "Y";
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (hasData == "Y" && rowIndex != 0) {
|
|
dataRow.ItemArray = row.Cells.ToArray();
|
|
|
|
dt.Rows.Add(dataRow);
|
|
rowNum++;
|
|
}
|
|
|
|
rowIndex++;
|
|
}
|
|
|
|
int totalRowNum = rowNum;
|
|
|
|
DataTable dt2 = dt.Clone();
|
|
|
|
if (totalRowNum < 6)
|
|
{
|
|
foreach (DataRow row in dt.Rows)
|
|
{
|
|
dt2.ImportRow(row);
|
|
}
|
|
}
|
|
else {
|
|
for (int i = 0; i < 3; i++) {
|
|
dt2.ImportRow(dt.Rows[i]);
|
|
}
|
|
|
|
for (int i = (totalRowNum - 3); i < totalRowNum; i++) {
|
|
dt2.ImportRow(dt.Rows[i]);
|
|
}
|
|
}
|
|
|
|
string datatableJsonString = "";
|
|
datatableJsonString = JsonConvert.SerializeObject(dt2);
|
|
objRet.data = datatableJsonString;
|
|
objRet.ret = "yes";
|
|
objRet.totalRecNum = totalRowNum;
|
|
objRet.xlsxSerial = xlsxFileCode;
|
|
json.WriteObject(context.Response.OutputStream, objRet);
|
|
return;
|
|
}
|
|
|
|
public bool IsReusable {
|
|
get {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public class result {
|
|
public string ret = "no";
|
|
public string err_code = "0000";
|
|
public string message = "";
|
|
public string xlsxSerial = "";
|
|
public int totalRecNum = 0;
|
|
public List<string> columns = new List<string>();
|
|
public List<col> cols = new List<col>();
|
|
public string data = "";
|
|
}
|
|
|
|
public class col {
|
|
public string data = "";
|
|
public string visible = "";
|
|
public string title = "";
|
|
}
|
|
|
|
} |