diff --git a/BarcodeXlsx.sln b/BarcodeXlsx.sln index c27e4f2..53d04af 100644 --- a/BarcodeXlsx.sln +++ b/BarcodeXlsx.sln @@ -5,7 +5,9 @@ MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BarcodeXlsx", "BarcodeXlsx\BarcodeXlsx.csproj", "{0B304859-4BAD-4587-A83A-B9EC64193680}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "barcode", "barcode\barcode.csproj", "{93B3605C-AE72-4A18-80E7-9510DC99A07E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "barcode", "barcode\barcode.csproj", "{93B3605C-AE72-4A18-80E7-9510DC99A07E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common", "Common\Common.csproj", "{B551F263-1950-4082-AD39-C6581B79AD28}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -25,10 +27,34 @@ {0B304859-4BAD-4587-A83A-B9EC64193680}.Debug|x86.Build.0 = Debug|Any CPU {0B304859-4BAD-4587-A83A-B9EC64193680}.Release|Any CPU.ActiveCfg = Release|Any CPU {0B304859-4BAD-4587-A83A-B9EC64193680}.Release|Any CPU.Build.0 = Release|Any CPU + {0B304859-4BAD-4587-A83A-B9EC64193680}.Release|x64.ActiveCfg = Release|Any CPU + {0B304859-4BAD-4587-A83A-B9EC64193680}.Release|x64.Build.0 = Release|Any CPU + {0B304859-4BAD-4587-A83A-B9EC64193680}.Release|x86.ActiveCfg = Release|Any CPU + {0B304859-4BAD-4587-A83A-B9EC64193680}.Release|x86.Build.0 = Release|Any CPU {93B3605C-AE72-4A18-80E7-9510DC99A07E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {93B3605C-AE72-4A18-80E7-9510DC99A07E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {93B3605C-AE72-4A18-80E7-9510DC99A07E}.Debug|x64.ActiveCfg = Debug|Any CPU + {93B3605C-AE72-4A18-80E7-9510DC99A07E}.Debug|x64.Build.0 = Debug|Any CPU + {93B3605C-AE72-4A18-80E7-9510DC99A07E}.Debug|x86.ActiveCfg = Debug|Any CPU + {93B3605C-AE72-4A18-80E7-9510DC99A07E}.Debug|x86.Build.0 = Debug|Any CPU {93B3605C-AE72-4A18-80E7-9510DC99A07E}.Release|Any CPU.ActiveCfg = Release|Any CPU {93B3605C-AE72-4A18-80E7-9510DC99A07E}.Release|Any CPU.Build.0 = Release|Any CPU + {93B3605C-AE72-4A18-80E7-9510DC99A07E}.Release|x64.ActiveCfg = Release|Any CPU + {93B3605C-AE72-4A18-80E7-9510DC99A07E}.Release|x64.Build.0 = Release|Any CPU + {93B3605C-AE72-4A18-80E7-9510DC99A07E}.Release|x86.ActiveCfg = Release|Any CPU + {93B3605C-AE72-4A18-80E7-9510DC99A07E}.Release|x86.Build.0 = Release|Any CPU + {B551F263-1950-4082-AD39-C6581B79AD28}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B551F263-1950-4082-AD39-C6581B79AD28}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B551F263-1950-4082-AD39-C6581B79AD28}.Debug|x64.ActiveCfg = Debug|Any CPU + {B551F263-1950-4082-AD39-C6581B79AD28}.Debug|x64.Build.0 = Debug|Any CPU + {B551F263-1950-4082-AD39-C6581B79AD28}.Debug|x86.ActiveCfg = Debug|Any CPU + {B551F263-1950-4082-AD39-C6581B79AD28}.Debug|x86.Build.0 = Debug|Any CPU + {B551F263-1950-4082-AD39-C6581B79AD28}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B551F263-1950-4082-AD39-C6581B79AD28}.Release|Any CPU.Build.0 = Release|Any CPU + {B551F263-1950-4082-AD39-C6581B79AD28}.Release|x64.ActiveCfg = Release|Any CPU + {B551F263-1950-4082-AD39-C6581B79AD28}.Release|x64.Build.0 = Release|Any CPU + {B551F263-1950-4082-AD39-C6581B79AD28}.Release|x86.ActiveCfg = Release|Any CPU + {B551F263-1950-4082-AD39-C6581B79AD28}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/BarcodeXlsx/BarcodeXlsx.csproj b/BarcodeXlsx/BarcodeXlsx.csproj index 65548c0..1f64914 100644 --- a/BarcodeXlsx/BarcodeXlsx.csproj +++ b/BarcodeXlsx/BarcodeXlsx.csproj @@ -9,9 +9,13 @@ - + - + + + + + diff --git a/BarcodeXlsx/Program.cs b/BarcodeXlsx/Program.cs index 8e2e1ee..7366da4 100644 --- a/BarcodeXlsx/Program.cs +++ b/BarcodeXlsx/Program.cs @@ -12,6 +12,8 @@ using ImageMagick; +using Common; + namespace BarcodeXlsx { class Program @@ -29,81 +31,23 @@ Console.WriteLine("--source {0}", param.sourceFileName); } - XLWorkbook book = new XLWorkbook(param.sourceFileName); - foreach (var sheet in book.Worksheets) - { - foreach (var cell in sheet.Cells()) - { - string cellValue = cell.GetString(); - if (cellValue.Length > param.preChars.Length + param.postChars.Length) - { - string preChars = cellValue.Substring(0, param.preChars.Length); - string postChears = cellValue.Substring(cellValue.Length - param.postChars.Length); - - if (preChars == param.preChars && postChears == param.postChars) - { - string barcodeData = cellValue.Substring(param.preChars.Length, cellValue.Length - param.preChars.Length - param.postChars.Length); - int delimiterPos = barcodeData.IndexOf(":"); - if (delimiterPos > 0) - { - string barcodeType = barcodeData.Substring(0, delimiterPos); - string barcodeValue = barcodeData.Substring(delimiterPos + 1); - - if (param.enabledVerbose) - { - Console.WriteLine("sheetName = {0}, cellAddress = {1}, barcodeType = {2}, barcodeValue = {3}", sheet.Name, cell.Address, barcodeType, barcodeValue); - } - - try - { - BarcodeLib.Barcode barcode = new BarcodeLib.Barcode(); - barcode.Height = param.imageHeight; - barcode.Width = param.imageWidht; - barcode.Alignment = BarcodeLib.AlignmentPositions.CENTER; - barcode.IncludeLabel = param.enabledLabel; - barcode.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER; - barcode.LabelFont = new Font(FontFamily.GenericSansSerif, 8); - barcode.BackColor = Color.White; - barcode.ImageFormat = ImageFormat.Bmp; - barcode.Encode(DecodeBarcodeStyle(barcodeType), barcodeValue); - - MemoryStream tempStream1 = new MemoryStream(); - barcode.EncodedImage.Save(tempStream1, ImageFormat.Png); - - tempStream1.Position = 0; - MagickImage image = new MagickImage(tempStream1); - image.Transparent(MagickColors.White); - MemoryStream tempStream2 = new MemoryStream(); - image.Write(tempStream2, MagickFormat.Png); - - var picture = sheet.AddPicture(tempStream2); - picture.MoveTo(cell); - picture.Scale(0.5, true); - picture.Height = (int)(cell.WorksheetRow().Height / 0.75); - picture.Width = (int)(cell.WorksheetColumn().Width / 0.118); - - if (param.enabledRemoveTag) - { - cell.SetValue(""); - } - } - catch (Exception exp) - { - Console.Error.WriteLine("{0}", exp.Message); - } - } - } - } - } - } + BarcodeXlsxImporter barcodeXlsx = new BarcodeXlsxImporter(); + barcodeXlsx.preChars = param.preChars; + barcodeXlsx.postChars = param.postChars; + barcodeXlsx.enabledVerbose = param.enabledVerbose; + barcodeXlsx.enabledProgress = param.enabledProgress; + barcodeXlsx.enabledLabel = param.enabledLabel; + barcodeXlsx.enabledRemoveTag = param.enabledRemoveTag; + barcodeXlsx.imageWidth = param.imageWidht; + barcodeXlsx.imageHeight = param.imageHeight; if (param.destinationFileName == null) { - book.Save(); + barcodeXlsx.Convert(param.sourceFileName); } else { - book.SaveAs(param.destinationFileName); + barcodeXlsx.Convert(param.sourceFileName, param.destinationFileName); } } catch (Exception exp) diff --git a/BarcodeXlsx/Properties/launchSettings.json b/BarcodeXlsx/Properties/launchSettings.json index b1dc9a4..4a429b5 100644 --- a/BarcodeXlsx/Properties/launchSettings.json +++ b/BarcodeXlsx/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "BarcodeXlsx": { "commandName": "Project", - "commandLineArgs": "-source C:\\Users\\07121\\Desktop\\Book1.xlsx -verbose -removetag" + "commandLineArgs": "-source D:\\dump\\barcodeXlsx\\sample.xlsx -verbose" } } } \ No newline at end of file diff --git a/Common/BarcodeXlsxImporter.cs b/Common/BarcodeXlsxImporter.cs new file mode 100644 index 0000000..9916abb --- /dev/null +++ b/Common/BarcodeXlsxImporter.cs @@ -0,0 +1,295 @@ +using System; +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; + +using ClosedXML; +using ClosedXML.Utils; +using ClosedXML.Excel; +using ClosedXML.Excel.Drawings; + +using ImageMagick; + +namespace Common +{ + public class BarcodeXlsxImporter + { + public string preChars = "{"; + public string postChars = "}"; + public bool enabledVerbose = false; + public bool enabledProgress = false; + public bool enabledLabel = false; + public bool enabledRemoveTag = false; + public int imageWidth = 256; + public int imageHeight = 64; + public int marginHeight = 4; + public int marginWidth = 4; + + public BarcodeXlsxImporter() + { + + } + public void Convert(string sourceFileName) + { + XLWorkbook book = new XLWorkbook(sourceFileName); + Convert(book); + book.Save(); + } + + public void Convert(string sourceFileName, string destFileName) + { + XLWorkbook book = new XLWorkbook(sourceFileName); + Convert(book); + book.SaveAs(destFileName); + } + + public void Convert(Stream inputStream, Stream outputStream) + { + XLWorkbook book = new XLWorkbook(inputStream); + Convert(book); + book.SaveAs(outputStream); + } + + public void Convert(XLWorkbook book) + { + foreach (var sheet in book.Worksheets) + { + foreach (var cell in sheet.Cells()) + { + string cellValue = cell.GetString(); + if (cellValue.Length > preChars.Length + postChars.Length) + { + string preChars = cellValue.Substring(0, this.preChars.Length); + string postChears = cellValue.Substring(cellValue.Length - this.postChars.Length); + + if (preChars == this.preChars && postChears == this.postChars) + { + string barcodeData = cellValue.Substring(preChars.Length, cellValue.Length - preChars.Length - postChars.Length); + int delimiterPos = barcodeData.IndexOf(":"); + if (delimiterPos > 0) + { + string barcodeType = barcodeData.Substring(0, delimiterPos); + string barcodeValue = barcodeData.Substring(delimiterPos + 1); + + try + { + BarcodeLib.Barcode barcode = new BarcodeLib.Barcode(); + barcode.Height = imageHeight - marginHeight * 2; + barcode.Width = imageWidth - marginWidth * 2; + barcode.Alignment = BarcodeLib.AlignmentPositions.CENTER; + barcode.IncludeLabel = enabledLabel; + barcode.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER; + barcode.LabelFont = new Font(FontFamily.GenericSansSerif, 8); + barcode.BackColor = Color.White; + barcode.ImageFormat = ImageFormat.Bmp; + barcode.Encode(DecodeBarcodeStyle(barcodeType), barcodeValue); + + MemoryStream tempStream1 = new MemoryStream(); + barcode.EncodedImage.Save(tempStream1, ImageFormat.Png); + + tempStream1.Position = 0; + MagickImage image = new MagickImage(tempStream1); + image.MatteColor = MagickColors.White; + image.Frame(marginWidth, marginHeight, 0, 0); + image.Transparent(MagickColors.White); + MemoryStream tempStream2 = new MemoryStream(); + image.Write(tempStream2, MagickFormat.Png); + + var picture = sheet.AddPicture(tempStream2); + picture.MoveTo(cell); + picture.Scale(0.5, true); + picture.Height = (int)(cell.WorksheetRow().Height / 0.75); + picture.Width = (int)(cell.WorksheetColumn().Width / 0.118); + + if (enabledRemoveTag) + { + cell.SetValue(""); + } + } + catch (Exception exp) + { + Console.Error.WriteLine("{0}", exp.Message); + } + } + } + } + } + } + } + + /// + /// Decode barcode style name to enum value + /// + /// + /// + public BarcodeLib.TYPE DecodeBarcodeStyle(string barcodeStyle) + { + BarcodeLib.TYPE barcodeType = BarcodeLib.TYPE.CODE128; + switch (barcodeStyle.ToUpper()) + { + case "UNSPECIFIED": + barcodeType = BarcodeLib.TYPE.UNSPECIFIED; + break; + + case "UPCA": + barcodeType = BarcodeLib.TYPE.UPCA; + break; + + case "UPCE": + barcodeType = BarcodeLib.TYPE.UPCE; + break; + + case "UPC_SUPPLEMENTAL_2DIGIT": + barcodeType = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_2DIGIT; + break; + + case "UPC_SUPPLEMENTAL_5DIGIT": + barcodeType = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_5DIGIT; + break; + + case "EAN13": + barcodeType = BarcodeLib.TYPE.EAN13; + break; + + case "EAN8": + barcodeType = BarcodeLib.TYPE.EAN8; + break; + + case "Interleaved2of5": + barcodeType = BarcodeLib.TYPE.Interleaved2of5; + break; + + case "Interleaved2of5_Mod10": + barcodeType = BarcodeLib.TYPE.Interleaved2of5_Mod10; + break; + + case "Standard2of5": + barcodeType = BarcodeLib.TYPE.Standard2of5; + break; + + case "Standard2of5_Mod10": + barcodeType = BarcodeLib.TYPE.Standard2of5_Mod10; + break; + + case "Industrial2of5": + barcodeType = BarcodeLib.TYPE.Industrial2of5; + break; + + case "Industrial2of5_Mod10": + barcodeType = BarcodeLib.TYPE.Industrial2of5_Mod10; + break; + + case "CODE39": + barcodeType = BarcodeLib.TYPE.CODE39; + break; + + case "CODE39Extended": + barcodeType = BarcodeLib.TYPE.CODE39Extended; + break; + + case "CODE39_Mod43": + barcodeType = BarcodeLib.TYPE.CODE39_Mod43; + break; + + case "Codabar": + barcodeType = BarcodeLib.TYPE.Codabar; + break; + + case "PostNet": + barcodeType = BarcodeLib.TYPE.PostNet; + break; + + case "BOOKLAND": + barcodeType = BarcodeLib.TYPE.BOOKLAND; + break; + + case "ISBN": + barcodeType = BarcodeLib.TYPE.ISBN; + break; + + case "JAN13": + barcodeType = BarcodeLib.TYPE.JAN13; + break; + + case "MSI_Mod10": + barcodeType = BarcodeLib.TYPE.MSI_Mod10; + break; + + case "MSI_2Mod10": + barcodeType = BarcodeLib.TYPE.MSI_2Mod10; + break; + + case "MSI_Mod11": + barcodeType = BarcodeLib.TYPE.MSI_Mod11; + break; + + case "MSI_Mod11_Mod10": + barcodeType = BarcodeLib.TYPE.MSI_Mod11_Mod10; + break; + + case "Modified_Plessey": + barcodeType = BarcodeLib.TYPE.Modified_Plessey; + break; + + case "CODE11": + barcodeType = BarcodeLib.TYPE.CODE11; + break; + + case "USD8": + barcodeType = BarcodeLib.TYPE.USD8; + break; + + case "UCC12": + barcodeType = BarcodeLib.TYPE.UCC12; + break; + + case "UCC13": + barcodeType = BarcodeLib.TYPE.UCC13; + break; + + case "LOGMARS": + barcodeType = BarcodeLib.TYPE.LOGMARS; + break; + + case "CODE128": + barcodeType = BarcodeLib.TYPE.CODE128; + break; + + case "CODE128A": + barcodeType = BarcodeLib.TYPE.CODE128A; + break; + + case "CODE128B": + barcodeType = BarcodeLib.TYPE.CODE128B; + break; + + case "CODE128C": + barcodeType = BarcodeLib.TYPE.CODE128C; + break; + + case "ITF14": + barcodeType = BarcodeLib.TYPE.ITF14; + break; + + case "CODE93": + barcodeType = BarcodeLib.TYPE.CODE93; + break; + + case "TELEPEN": + barcodeType = BarcodeLib.TYPE.TELEPEN; + break; + + case "FIM": + barcodeType = BarcodeLib.TYPE.FIM; + break; + + case "PHARMACODE": + barcodeType = BarcodeLib.TYPE.PHARMACODE; + break; + + } + + return barcodeType; + } + } +} diff --git a/Common/Common.csproj b/Common/Common.csproj new file mode 100644 index 0000000..6200ca9 --- /dev/null +++ b/Common/Common.csproj @@ -0,0 +1,13 @@ + + + + netcoreapp2.2 + + + + + + + + + diff --git a/barcode/Controllers/HomeController.cs b/barcode/Controllers/HomeController.cs index d245aea..a310257 100644 --- a/barcode/Controllers/HomeController.cs +++ b/barcode/Controllers/HomeController.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; + using barcode.Models; @@ -17,9 +18,9 @@ { if (this.Request.Method == "POST") { - foreach (var form in this.Request.Form.Files) + foreach (var file in this.Request.Form.Files) { - Debug.WriteLine("Index, {0}, {1}", form.Key, form.Value); + file.OpenReadStream(); } }