国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

csharp: read excel using Aspose.Cells

2019-11-17 02:33:05
字體:
來源:轉載
供稿:網友

csharp: read Excel using aspose.Cells

 /// <summary>        ///         /// </summary>        /// <param name="strFileName"></param>        /// <returns></returns>        public static System.Data.DataTable ReadExcel(String strFileName)        {            Workbook book = new Workbook(strFileName);            //book.Open(strFileName); //老版本            Worksheet sheet = book.Worksheets[0];                        Cells cells = sheet.Cells;            return cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true);        }        /// <summary>        ///         /// </summary>        /// <param name="strFileName"></param>        /// <param name="sheetname"></param>        /// <returns></returns>        public static System.Data.DataTable ReadExcel(String strFileName,string sheetname)        {            Workbook book = new Workbook(strFileName);            //book.Open(strFileName);//老版本            Worksheet sheet = book.Worksheets[sheetname];            Cells cells = sheet.Cells;            return cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true);        }        /// <summary>        /// 讀取工作表        /// 涂聚文        /// 20150228        /// </summary>        /// <param name="strFileName"></param>        /// <param name="comb"></param>        public static void ReadExcelCombox(String strFileName, System.Windows.Forms.ComboBox comb)        {            comb.Items.Clear();            Workbook book = new Workbook(strFileName);            // book.Open(strFileName);//老版本            Worksheet sheet = book.Worksheets[0];            for (int i = 0; i < book.Worksheets.Count; i++)            {                comb.Items.Add(book.Worksheets[i].Name.ToString());              }           // Cells cells = sheet.Cells;            //return cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true);        }        /// <summary>        /// DataTable導出到EXCEL        /// http://www.aspose.com/docs/display/cellsnet/Aspose.Cells+Object+Model        /// http://www.aspose.com/docs/display/cellsnet/Converting+Worksheet+to+Image+and+Worksheet+to+Image+by+Page        /// </summary>        /// <param name="datatable"></param>        /// <param name="filepath"></param>        /// <param name="error"></param>        /// <returns></returns>        public static bool DataTableToExcel(DataTable datatable, string filepath, out string error)        {            error = "";            try            {                if (datatable == null)                {                    error = "DataTableToExcel:datatable 為空";                    return false;                }                Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();                Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];                Aspose.Cells.Cells cells = sheet.Cells;                int nRow = 0;                foreach (DataRow row in datatable.Rows)                {                    nRow++;                    try                    {                        for (int i = 0; i < datatable.Columns.Count; i++)                        {                            if (row[i].GetType().ToString() == "System.Drawing.Bitmap")                            {                                //------插入圖片數據-------                                System.Drawing.Image image = (System.Drawing.Image)row[i];                                MemoryStream mstream = new MemoryStream();                                image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg);                                sheet.Pictures.Add(nRow, i, mstream);                            }                            else                            {                                cells[nRow, i].PutValue(row[i]);                            }                        }                    }                    catch (System.Exception e)                    {                        error = error + " DataTableToExcel: " + e.Message;                    }                }                workbook.Save(filepath);                return true;            }            catch (System.Exception e)            {                error = error + " DataTableToExcel: " + e.Message;                return false;            }        }        /// <summary>        /// 工作表轉為圖片        /// </summary>        /// <param name="file">來源EXCEL文件</param>        /// <param name="sheetname">工作表名</param>        /// <param name="toimagefile">生成圖片文件</param>        public static void CellConverImge(string file, string sheetname, string toimagefile)        {            //Create a new Workbook object and            //Open a template Excel file.            Workbook book = new Workbook(file);            //Get the first worksheet.            Worksheet sheet = book.Worksheets[sheetname];            //Define ImageOrPRintOptions            ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();            //Specify the image format            imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;            //Only one page for the whole sheet would be rendered            imgOptions.OnePagePerSheet = true;            //Render the sheet with respect to specified image/print options            SheetRender sr = new SheetRender(sheet, imgOptions);            //Render the image for the sheet            Bitmap bitmap = sr.ToImage(0);            //Save the image file specifying its image format.            bitmap.Save(toimagefile);        }        /// <summary>        ///         /// </summary>        /// <param name="sURL"></param>        /// <param name="toExcelFile"></param>        public static void LoadUrlImage(string sURL,string toExcelFile)        {            //Define memory stream object            System.IO.MemoryStream objImage;            //Define web client object            System.Net.WebClient objwebClient;            //Define a string which will hold the web image url            //string sURL = "http://files.myOpera.com/Mickeyjoe_irl/albums/38458/abc.jpg";            try            {                //Instantiate the web client object                objwebClient = new System.Net.WebClient();                //Now, extract data into memory stream downloading the image data into the array of bytes                objImage = new System.IO.MemoryStream(objwebClient.DownloadData(sURL));                //Create a new workbook                Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();                //Get the first worksheet in the book                Aspose.Cells.Worksheet sheet = wb.Worksheets[0];                //Get the first worksheet pictures collection                Aspose.Cells.Drawing.PictureCollection pictures = sheet.Pictures;                //Insert the picture from the stream to B2 cell                pictures.Add(1, 1, objImage);                //Save the excel file  "d://test//webimagebook.xls"                wb.Save(toExcelFile);            }            catch (Exception ex)            {                //Write the error message on the console                Console.WriteLine(ex.Message);            }        }        /// <summa  /// <summary>        /// 涂聚文        /// 20150228        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnFile_Click(object sender, EventArgs e)        {                  try                  {                //bool imail = false;                this.Cursor = Cursors.WaitCursor;                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);                //JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif                openFileDialog1.FileName = "";                openFileDia
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 太湖县| 安丘市| 滁州市| 关岭| 竹山县| 嘉峪关市| 达拉特旗| 饶平县| 依兰县| 新蔡县| 贡嘎县| 金溪县| 确山县| 义乌市| 汕头市| 惠安县| 婺源县| 汉阴县| 九龙坡区| 莒南县| 文安县| 晋城| 蓬溪县| 建阳市| 永城市| 宜兰县| 陈巴尔虎旗| 香港| 吉林市| 江津市| 措美县| 石阡县| 昌宁县| 疏附县| 宜良县| 洛宁县| 浪卡子县| 南皮县| 四平市| 凤山市| 巴彦淖尔市|