[ExtJs4.0]数据从excle2003导入到数据库【2-1】

public ActionResult ImportExcel()

        {

            try

            {

                int sheetIndex = 0;

                int headerRowIndex = 0;

                DataTable table = new DataTable();

                if (Request.ContentLength > 2 * 1024 * 1024)

                {

                    return ExtjsFormFail("¦?ä??t¬?䨮¢?ê?");

                }

                HttpPostedFileBase postedFile = Request.Files["file"];//䨮¡ã¬¡§?¨??t

                if (postedFile != null)

                {

                    //D??t¤?1?º?¤?y¨¡¤

                    string fileExtension = Path.GetExtension(Path.GetFileName(postedFile.FileName));

                    if (fileExtension == null || fileExtension.ToLower() != ".xls")//&& fileExtension != ".xlsx" ?¡ã.Net?¡ì?Execl2007

                    {

                        return ExtjsFormFail("?¦?ä?y¨¡¤Ì??t?º?êoxlsê?");

                    }

                    //ä?Ì?¦Ìª3¢¨´º¡À?tD?

                    string filePath = Path.GetTempFileName();

                    postedFile.SaveAs(filePath);//¦?ä??t

                    FileStream readStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                    HSSFWorkbook workbook = new HSSFWorkbook(readStream);

                    HSSFSheet sheet = workbook.GetSheetAt(sheetIndex);

                    HSSFRow headerRow = sheet.GetRow(headerRowIndex);

                    int cellCount = headerRow.LastCellNum;

                    for (int i = headerRow.FirstCellNum; i < cellCount; i++)

                    {

                        bool hand = true;

                        DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);

                        for (int k = 0; k < table.Columns.Count; k++)

                        {

                            if (column.ToString() == table.Columns[k].Caption)

                            {

                                hand = false;

                            }

                        }

                        if (hand)

                        {

                            table.Columns.Add(column);

                        }

                        else

                        {

                            DataColumn column1 = new DataColumn(column + i.ToString());

                            table.Columns.Add(column1);

                        }

                    }

                    int rowCount = sheet.LastRowNum;

                    for (int i = (sheet.FirstRowNum + 1); i <= rowCount; i++)

                    {

                        HSSFRow row = sheet.GetRow(i);

                        DataRow dataRow = table.NewRow();

                        for (int j = row.FirstCellNum; j < cellCount; j++)

                        {

                            if (row.GetCell(j) != null)

                              dataRow[j] = row.GetCell(j).ToString();

                        }

                        table.Rows.Add(dataRow);

                    }

                }

                else

                {

                    return ExtjsFormFail("???¨²°aÌ?¨?Ì??t");

                }

               

你可能感兴趣的:(ExtJs,js导入,导入判断)