程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 使用Aspose插件對Excel操作,aspose插件excel

使用Aspose插件對Excel操作,aspose插件excel

編輯:C#入門知識

使用Aspose插件對Excel操作,aspose插件excel


使用使用Aspose插件對Excel文檔進行導入導出操作

使用前請先下載Aspose插件引用

Excel導入:

 

前台使用file標簽獲取,submit方式提交。

<form id="form1" enctype="multipart/form-data" method="post" >
 <table class="table-condensed">
<tr>
                    <td class="text-right">導入表格:</td>
                    <td class="text-left"><input type="file" name="file1" class="btn btn-default btn-lg" /></td>
</tr>
<tr>
                    <td class="text-left">
                        <input type="submit" id="btnImport" name="btnImport" value="導入" class="btn btn-default" />
                    </td>
                </tr>
</table>

</form>

後台接收:

HttpPostedFileBase fileBase = Request.Files["file1"];//這裡獲取名稱與前台標簽name保持一致
if (fileBase != null)
            {
                string filename = Path.GetFileName(fileBase.FileName);
                string extension = Path.GetExtension(filename);
                string path = "/Upload/Test/" + DateTime.Now.ToString("yyyyMMdd") + "/";
                Directory.CreateDirectory(Path.GetDirectoryName(Request.MapPath(path)));
                string newFilename = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                string fullFileName = path + newFilename + extension;
                fileBase.SaveAs(Request.MapPath(fullFileName));
          try
                {
            Stopwatch sw = new Stopwatch();//記錄導入操作用時多長
                    sw.Start();
//這裡可放入BLL方法處理
string result = new ProductBLL().ImportExcel(Request.MapPath(path), newFilename, extension);

//BLL方法 ProductBLL
public string ImportExcel(string path, string filename, string extension)
        {
            Workbook workbook = new Workbook(path + filename + extension);
            Worksheet worksheet = workbook.Worksheets[0];
            Cells cells = worksheet.Cells;
            for (int i = 1; i < cells.Rows.Count; i++)
            {
                try
                {
                    string brand = cells[i, 0].StringValue.Trim();//獲取列值
                    string years = cells[i, 1].StringValue.Trim();
                }
                catch (Exception e)
                {
                    continue;
                }

            }
            return "OK";
        }         sw.Stop(); long runTime = sw.ElapsedMilliseconds / 1000; //獲取到操作用時多少秒       }       catch (Exception e) { Log.Write("導入", "導入錯誤", "錯誤信息:" + e.Message); } }

Excel導出:
string path = "/Upload/Test/" + DateTime.Now.ToString("yyyyMMdd") + "/";
            Directory.CreateDirectory(Path.GetDirectoryName(Server.MapPath(path)));
            string newFilename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls";
            string fullFileName = Server.MapPath(path + newFilename);

public void ExportInfo(List<Test> list, string fullFileName)
        {
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
            Aspose.Cells.Worksheet cellSheet = workbook.Worksheets[0];

            cellSheet.PageSetup.LeftMargin = 0.3;//左邊距
            cellSheet.PageSetup.RightMargin = 0.3;//右邊距
            cellSheet.PageSetup.TopMargin = 1;//上邊距
            cellSheet.PageSetup.BottomMargin = 0.5;//下邊距
            cellSheet.PageSetup.FooterMargin = 0.5;//頁腳
            cellSheet.PageSetup.HeaderMargin = 0.5;//頁眉
            cellSheet.PageSetup.Orientation = PageOrientationType.Landscape;
            cellSheet.PageSetup.CenterHorizontally = true;//水平居中
            cellSheet.PageSetup.CenterVertically = true;

cellSheet.Cells[0, 0].PutValue("貨號");
            cellSheet.Cells[0, 1].PutValue("顏色");
            cellSheet.Cells[0, 2].PutValue("尺碼");

int i = 1;
            foreach (var item in list)
            {
cellSheet.Cells[i, 0].PutValue(item.productno);
                cellSheet.Cells[i, 1].PutValue(item.size);
                cellSheet.Cells[i, 2].PutValue(item.color);
i++;
            }
cellSheet.AutoFitColumns();

            fullFileName = Path.GetFullPath(fullFileName);
            workbook.Save(fullFileName);
}    

return File(fullFileName, "application/ms-excel", UserName + "_Test單" + newFilename);// 方法Action裡直接返回File文件下載。

//DataTable數據源導出,封裝ToExcel方法。
da.ToExcel(fullFileName);
return File(fullFileName, "application/ms-excel", UserName + "_Test單" + newFilename);

     /// <summary>
        /// DataTable數據表保存至Excel
        /// </summary>
        /// <param name="dt">數據源</param>
        /// <param name="fullFileName">文件完整路徑</param>
        public static void ToExcel(this System.Data.DataTable dt, string fullFileName)
        {

            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
            Aspose.Cells.Worksheet cellSheet = workbook.Worksheets[0];

            cellSheet.Name = dt.TableName;

            int rowIndex = 0;
            int colIndex = 0;
            int colCount = dt.Columns.Count;
            int rowCount = dt.Rows.Count;

            //列名的處理
            for (int i = 0; i < colCount; i++)
            {
                cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Columns[i].ColumnName);
                //cellSheet.Cells[rowIndex, colIndex].SetStyle.Font.IsBold = true;
                //cellSheet.Cells[rowIndex, colIndex].Style.Font.Name = "宋體";
                colIndex++;
            }

            Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];
            style.Font.Name = "Arial";
            style.Font.Size = 10;
            Aspose.Cells.StyleFlag styleFlag = new Aspose.Cells.StyleFlag();
            cellSheet.Cells.ApplyStyle(style, styleFlag);

            rowIndex++;

            for (int i = 0; i < rowCount; i++)
            {
                colIndex = 0;
                for (int j = 0; j < colCount; j++)
                {
                    cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Rows[i][j].ToString());
                    colIndex++;
                }
                rowIndex++;
            }
            cellSheet.AutoFitColumns();

            fullFileName = Path.GetFullPath(fullFileName);
            workbook.Save(fullFileName);
        }
 由於抽取部分代碼出來,排版和引用方面未做詳細注釋,前台和後台方法對應即可。

 
做個筆記日後方便自己查看使用。做個筆記日後方便自己查看使用。做個筆記日後方便自己查看使用。做個筆記日後方便自己查看使用。

 
 
 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved