public void bw_makeArt(object sender, EventArgs e) { //read input image Bitmap bm = new Bitmap(ImagePath); //create a new excel document Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWorkbook = xlApp.Workbooks.Add(); Excel._Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1); xlWorksheet.Unprotect(); xlWorksheet.StandardWidth = 20 / 7.25; Excel.Range xlRange = xlWorksheet.UsedRange; //i = across, j = up, image coordinates start from bottom left corner whereas excel starts from top left for (int j = 0; j < bm.Height - 1; j++) { for (int i = 0; i < bm.Width - 1; i++) { xlRange.Cells[j + 1, i + 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(bm.GetPixel(i, j)); } Debug.WriteLine("{0} out of {1}", j * bm.Width, bm.Width * bm.Height); } xlWorkbook.SaveAs(OutputPath); xlApp.Quit(); }
public override void WriteSheet(string sheetName, DataTable dt) { Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(this.FullPathFile, ReadOnly: false); int workScheduleSheetIndex = GetSheeIndex(xlWorkbook, sheetName); if (workScheduleSheetIndex == 0) { throw new SheetNotFoundException(); } Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[workScheduleSheetIndex]; if (!String.IsNullOrEmpty(this.SheetPassword)) { xlWorksheet.Unprotect(this.SheetPassword); } for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { xlWorksheet.Cells[i + 2, j + 1] = dt.Rows[i][j].ToString(); } } if (!String.IsNullOrEmpty(this.SheetPassword)) { xlWorksheet.Protect(this.SheetPassword); } xlWorkbook.Save(); xlWorkbook.Close(); }
public static bool UnLockExcelInterop(string filepath, int sheet_index, string password, out String err) { Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filepath); Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[sheet_index]; err = ""; try { //Create COM Objects. Create a COM object for everything that is referenced xlWorksheet.Unprotect(password); xlApp.DisplayAlerts = false; xlWorkbook.SaveAs(filepath); xlApp.DisplayAlerts = true; //注意: Excel是Unmanaged程式,要妥善結束才能乾淨不留痕跡 //否則,很容易留下一堆excel.exe在記憶體中 //所有用過的COM+物件都要使用Marshal.FinalReleaseComObject清掉 //COM+物件的Reference Counter,以利結束物件回收記憶體 if (xlWorksheet != null) { Marshal.FinalReleaseComObject(xlWorksheet); } if (xlWorkbook != null) { xlWorkbook.Close(false); //忽略尚未存檔內容,避免跳出提示卡住 Marshal.FinalReleaseComObject(xlWorkbook); } if (xlApp != null) { xlApp.Workbooks.Close(); xlApp.Quit(); Marshal.FinalReleaseComObject(xlApp); } //cleanup GC.Collect(); GC.WaitForPendingFinalizers(); return(true); } catch (Exception ex) { //注意: Excel是Unmanaged程式,要妥善結束才能乾淨不留痕跡 //否則,很容易留下一堆excel.exe在記憶體中 //所有用過的COM+物件都要使用Marshal.FinalReleaseComObject清掉 //COM+物件的Reference Counter,以利結束物件回收記憶體 if (xlWorksheet != null) { Marshal.FinalReleaseComObject(xlWorksheet); } if (xlWorkbook != null) { xlWorkbook.Close(false); //忽略尚未存檔內容,避免跳出提示卡住 Marshal.FinalReleaseComObject(xlWorkbook); } if (xlApp != null) { xlApp.Workbooks.Close(); xlApp.Quit(); Marshal.FinalReleaseComObject(xlApp); } //cleanup GC.Collect(); GC.WaitForPendingFinalizers(); return(false); } }
public void bw_makeArt(object sender, EventArgs e) { //read input image Bitmap bm = new Bitmap(ImagePath); //create a new excel document Excel.Workbook xlWorkbook = xlApp.Workbooks.Add(); Excel._Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1); xlWorksheet.Unprotect(); xlWorksheet.StandardWidth = 20 / 7.25; Excel.Range xlRange = xlWorksheet.UsedRange; ColorSetter cs = new ColorSetter(xlRange); //assign it here rather than in the for loop to avoid multithreading calamities var Width = bm.Width; var Height = bm.Height; decimal totalPixels = (bm.Width - 1) * (bm.Height - 1); decimal pixelCounter = 0; //i = across, j = up, image coordinates start from bottom left corner whereas excel starts from top left Parallel.For(0, Height - 1, (j, loopState) => { var bmClone_ = bm.Clone(); Bitmap bmClone = ((Bitmap)(bmClone_)); for (int i = 0; i < Width - 1; i++) { //make sure a cancel hasn't been requested if (!Quitting) { try { cs.setColor(i, j, ref bmClone); pixelCounter++; } catch { } } else { //stops all threads from executing for clean exit when cancel is called loopState.Stop(); break; } } if (Quitting) { loopState.Stop(); } string msg = pixelCounter + "/" + totalPixels; Debug.WriteLine(msg); decimal progress = ((pixelCounter / totalPixels) * 100); int progressInt = Convert.ToInt32(progress); bw.ReportProgress(progressInt); }); if (!Quitting) { xlWorkbook.SaveAs(OutputPath); bw.ReportProgress(101); } xlRange = cs.Finish(); xlWorkbook.Close(0); xlApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); }