示例#1
0
        public void copyanhKHtunghang(string filecanlay)
        {
            try
            {
                Workbook workbook = new Workbook();
                workbook.LoadFromFile(filecanlay);
                if (!Directory.Exists(duongdanluuanh))
                {
                    Directory.CreateDirectory(duongdanluuanh);
                }
                foreach (Worksheet sheet in workbook.Worksheets)
                {
                    if (sheet.Pictures.Count > 0)
                    {
                        string tenanhH = "";
                        for (int i = 1; i < sheet.Pictures.Count; i++)
                        {
                            Spire.Xls.ExcelPicture anh = sheet.Pictures[i];
                            tenanhH = sheet.Range[anh.TopRow, 5].Value2.ToString();
                            if (!File.Exists(duongdanluuanh + @"\" + tenanhH + ".png"))
                            {
                                anh.Picture.Save(duongdanluuanh + @"\" + tenanhH + ".png", ImageFormat.Png);
                            }
                        }
                    }
                }

                workbook.Dispose();
            }
            catch (Exception e)
            {
                ghiloi.WriteLogError(e);
                return;
            }
        }
示例#2
0
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog chonfile = new OpenFileDialog();

            chonfile.Filter = "Mời các anh chọn file excel (*.xlsx)|*.xlsx";

            if (chonfile.ShowDialog() == DialogResult.OK)
            {
                //dung thu vien epplus
                ExcelPackage   filechon = new ExcelPackage(new FileInfo(chonfile.FileName));
                ExcelWorksheet ws       = filechon.Workbook.Worksheets[1];

                ExcelDrawing hinh1 = ws.Drawings[0];
                // var hinh2 = ws.Drawings[1];
                int cotdau   = hinh1.From.Column;
                int dongdau  = hinh1.From.Row;
                int cotcuoi  = hinh1.To.Column;
                int dongcuoi = hinh1.To.Row;
                txtnhap.Text = cotdau.ToString() + " " + cotcuoi.ToString() + " " + dongdau.ToString() + " " + dongcuoi.ToString();
                // dung thu vien spire
                Workbook workbook = new Workbook();

                workbook.LoadFromFile(chonfile.FileName);

                Worksheet sheet = workbook.Worksheets[1];
                int       soanh = sheet.Pictures.Count();
                Spire.Xls.ExcelPicture picture = sheet.Pictures[1];
                foreach (var anh in sheet.Pictures)
                {
                }
                picture.Picture.Save(@"image.png", ImageFormat.Png);
                lbthongbao.Text = "ok so anh la:" + soanh.ToString();
            }
        }
示例#3
0
        public void copyanhKHtunghang(string filecanlay)
        {
            var    excelApp       = new excel.Application();
            var    wb             = excelApp.Workbooks.Open(filecanlay);
            var    ws             = (excel.Worksheet)wb.Worksheets[1];
            string duongdanluuanh = duongdanapp + @"\luuanh";
            int    hangbatdau     = 0;

            List <string> tenanh = new List <string>();

            foreach (var pic in ws.Pictures())
            {
                hangbatdau = pic.TopLeftCell.Row;

                tenanh.Add(ws.Cells[hangbatdau, 2].value);
            }

            string[] manganh = tenanh.ToArray();

            wb.Close();
            excelApp.Quit();
            Marshal.FinalReleaseComObject(excelApp);
            Marshal.FinalReleaseComObject(wb);


            Workbook workbook = new Workbook();

            workbook.LoadFromFile(filecanlay);

            Worksheet sheet = workbook.Worksheets[0];

            if (!Directory.Exists(duongdanluuanh))
            {
                Directory.CreateDirectory(duongdanluuanh);
            }

            for (int i = 1; i < manganh.Length; i++)
            {
                Spire.Xls.ExcelPicture picture = sheet.Pictures[i];
                if (!File.Exists(duongdanluuanh + @"\" + manganh[i] + ".png"))
                {
                    picture.Picture.Save(duongdanluuanh + @"\" + manganh[i] + ".png", ImageFormat.Png);
                }
            }
            workbook.Dispose();
        }
示例#4
0
        /// <summary>
        /// nhan phim
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog chonfile = new OpenFileDialog();

            chonfile.Filter = "Mời các anh chọn file excel (*.xls)|*.xls";
            if (chonfile.ShowDialog() == DialogResult.OK)
            {
                var excelApp = new excel.Application();
                var wb       = excelApp.Workbooks.Open(chonfile.FileName);
                var ws       = (excel.Worksheet)wb.Worksheets[2];

                int hangbatdau = 0;

                List <string> tenanh = new List <string>();
                foreach (var pic in ws.Pictures())
                {
                    hangbatdau = pic.TopLeftCell.Row;

                    tenanh.Add(ws.Cells[hangbatdau, 5].value);
                }

                string[] manganh = tenanh.ToArray();
                excelApp.Quit();

                Workbook workbook = new Workbook();
                workbook.LoadFromFile(chonfile.FileName);

                Worksheet sheet = workbook.Worksheets[1];


                for (int i = 1; i < manganh.Length; i++)
                {
                    Spire.Xls.ExcelPicture picture = sheet.Pictures[i];
                    picture.Picture.Save(manganh[i] + ".png", ImageFormat.Png);
                }
            }
        }
示例#5
0
        public void copyanhvathongtin(string filecanlay)
        {
            var    excelApp       = new excel.Application();
            var    wb             = excelApp.Workbooks.Open(filecanlay);
            var    ws             = (excel.Worksheet)wb.Worksheets[2];
            string duongdanluuanh = duongdanapp + @"\luuanh";
            int    hangbatdau     = 0;
            //lay ngay tu file excel roi chuyen doi sang dinh dang khac truoc khi insert vao database
            string          ngayduocban = null;
            string          ngaydangso  = null;
            MatchCollection mat         = Regex.Matches(Convert.ToString(ws.Cells[7, 1].value2) ?? "", maungay);

            foreach (Match m in mat)
            {
                ngayduocban = m.Value.ToString();
            }
            ngaydangso = chuyendoingayvedangso(ngayduocban);

            List <string> tenanh = new List <string>();
            string        mahang, mota, bst, ghichu;

            foreach (var pic in ws.Pictures())
            {
                hangbatdau = pic.TopLeftCell.Row;

                tenanh.Add(ws.Cells[hangbatdau, 5].value);
            }
            int lastRow = ws.Cells[ws.Rows.Count, 5].End(excel.XlDirection.xlUp).Row;

            for (int i = 10; i < (lastRow + 2); i++)
            {
                if (ws.Cells[i, 5].value == null)
                {
                    continue;
                }
                mahang = ws.Cells[i, 5].value2.ToString();
                mota   = ws.Cells[i, 6].value2.ToString();
                bst    = Convert.ToString(ws.Cells[i, 10].value2);
                ghichu = Convert.ToString(ws.Cells[i, 11].value2);

                luuthongtin.Add(new laythongtin(ngayduocban, mahang, mota, bst, ghichu, ngaydangso));
            }

            string[] manganh = tenanh.ToArray();

            wb.Close();
            excelApp.Quit();
            Marshal.FinalReleaseComObject(excelApp);
            Marshal.FinalReleaseComObject(wb);


            Workbook workbook = new Workbook();

            workbook.LoadFromFile(filecanlay);

            Worksheet sheet = workbook.Worksheets[1];

            if (!Directory.Exists(duongdanluuanh))
            {
                Directory.CreateDirectory(duongdanluuanh);
            }

            for (int i = 1; i < manganh.Length; i++)
            {
                Spire.Xls.ExcelPicture picture = sheet.Pictures[i];
                if (!File.Exists(duongdanluuanh + @"\" + manganh[i] + ".png"))
                {
                    picture.Picture.Save(duongdanluuanh + @"\" + manganh[i] + ".png", ImageFormat.Png);
                }
            }
            workbook.Dispose();
        }
示例#6
0
        public void copyanhvathongtin(string filecanlay)
        {
            try
            {
                Workbook workbook = new Workbook();
                workbook.LoadFromFile(filecanlay);
                if (!Directory.Exists(duongdanluuanh))
                {
                    Directory.CreateDirectory(duongdanluuanh);
                }
                foreach (Worksheet sheet in workbook.Worksheets)
                {
                    if (Regex.IsMatch(sheet.Range[7, 1].Value2.ToString(), maungay))
                    {
                        string ngayduocban = null;
                        string ngaydangso  = null;
                        Match  layngay     = Regex.Match(sheet.Range[7, 1].Value2.ToString(), maungay);
                        ngayduocban = layngay.Value;
                        if (Regex.IsMatch(ngayduocban, @"\d{2}/\d{1}/\d{4}"))
                        {
                            ngayduocban = ngayduocban.Substring(0, 3) + "0" + ngayduocban.Substring(3, 6);
                        }
                        ngaydangso = chuyendoingayvedangso(ngayduocban);

                        string mahang, mota, bst, ghichu;
                        int    dongcuoi = sheet.LastRow;
                        for (int i = sheet.LastRow; i >= 0; i--)
                        {
                            CellRange cr = sheet.Rows[i - 1].Columns[1];
                            if (!cr.IsBlank)
                            {
                                dongcuoi = i;
                                break;
                            }
                        }
                        for (int i = 10; i < dongcuoi + 5; i++)
                        {
                            if (sheet.Range[i, 5].Value == null || sheet.Range[i, 5].Value == "")
                            {
                                continue;
                            }
                            mahang = sheet.Range[i, 5].Value2.ToString();
                            mota   = sheet.Range[i, 6].Value2.ToString();
                            bst    = sheet.Range[i, 10].Value2.ToString();
                            ghichu = sheet.Range[i, 11].Value2.ToString();
                            luuthongtin.Add(new laythongtin(ngayduocban, mahang, mota, bst, ghichu, ngaydangso));
                        }
                        string tenanhH = "";
                        for (int i = 1; i < sheet.Pictures.Count; i++)
                        {
                            Spire.Xls.ExcelPicture anh = sheet.Pictures[i];
                            tenanhH = sheet.Range[anh.TopRow, 5].Value2.ToString();
                            if (!File.Exists(duongdanluuanh + @"\" + tenanhH + ".png"))
                            {
                                anh.Picture.Save(duongdanluuanh + @"\" + tenanhH + ".png", ImageFormat.Png);
                            }
                        }
                    }
                }

                workbook.Dispose();
            }
            catch (Exception ex)
            {
                ghiloi.WriteLogError(ex);
                return;
            }
        }