Spire.XLS 在程序中直接打印excel
上代码
if (tbPrintSetBindingSource.Current == null) return; var item_TbPrintSet = tbPrintSetBindingSource.Current as RMES.IBatisEntity.TbPrintSet; fileTempPath = Path.Combine(Environment.CurrentDirectory, item_TbPrintSet.CWorkName); if (!File.Exists(fileTempPath)) { DevExpress.XtraEditors.XtraMessageBox.Show(string.Format("模板{0}文件不存在", item_TbPrintSet.CWorkName), "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } workbook = new Workbook(); if (!string.IsNullOrEmpty(item_TbPrintSet.CPwd.Trim())) { workbook.OpenPassword = item_TbPrintSet.CPwd; } workbook.LoadFromFile(fileTempPath); worksheet = workbook.Worksheets.Where(m => m.Name == item_TbPrintSet.CSheetName).FirstOrDefault(); if (worksheet == null) { DevExpress.XtraEditors.XtraMessageBox.Show(string.Format("模板文件中Sheet{0}不存在", item_TbPrintSet.CSheetName), "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } //移除其它数据 List<Spire.Xls.Core.IWorksheet> lstsheet = new List<Spire.Xls.Core.IWorksheet>(); workbook.Worksheets.All(m => { if (m.Name != item_TbPrintSet.CSheetName) { lstsheet.Add(m); } return true; }); lstsheet.All(m => { m.Remove(); return true; }); if (lst_TbPrintSetItem == null) { DevExpress.XtraEditors.XtraMessageBox.Show("模板数据项目没有配置", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (lst_TbPrintSetItem.Count == 0) { DevExpress.XtraEditors.XtraMessageBox.Show("模板数据项目没有配置", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (lst_TbPrintSetItem.Where(m => string.IsNullOrEmpty(m.CFiledName)).Count() > 0) { DevExpress.XtraEditors.XtraMessageBox.Show("模板数据映射中有映射列为空数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (DevExpress.XtraEditors.XtraMessageBox.Show(string.Format("是否打印模板{0},打印份数{1}?", item_TbPrintSet.CSheetName,txtPrintNum.Text.Trim()), "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No) return; //设置值 int rows_item = lst_TbPrintSetItem.Count; int rows = 0; //几行几列 int cols = 0; int rowStart = 0; //从几开开始 int colStart = 0; bool printLen = false; //是否打印长度 int rowSpan = 0; //列间隔 int colSpan = 0; string lenChar = string.Empty;//规格分隔符 rows = Convert.ToInt32(item_TbPrintSet.CRow); cols = Convert.ToInt32(item_TbPrintSet.CCol); printLen = item_TbPrintSet.CLen == "Y" ? true : false; rowStart = Convert.ToInt32(item_TbPrintSet.CRowStart); colStart = Convert.ToInt32(item_TbPrintSet.CColStart); rowSpan = Convert.ToInt32(item_TbPrintSet.CRowSpan); colSpan = Convert.ToInt32(item_TbPrintSet.CColSpan); lenChar = string.Format("{0}", item_TbPrintSet.CLenChar.Trim()); int n = 65; //从A开始 int index = 1; //从1行开始 n += colStart; //从A B C 开始 index += rowStart; int nl = n; //临时变量存入列A\B\C //循环行数 for (int i = 0; i < rows; i++) { nl = n; for (int k = 0; k < cols; k++) { //遍历几个数 for (int j = 0; j < rows_item; j++) { string range = string.Format("{0}{1}", ASIToCharacter(nl), index + j); //Console.WriteLine(range); worksheet.Range[range].Text = getTextEdit(lst_TbPrintSetItem[j].CFiledName, printLen, lenChar); } nl = nl + 1 + colSpan; } index += rowSpan; index += rows_item; } workbook.SaveToFile(fileSavePath); worksheet.PageSetup.PaperSize = PaperSizeType.PaperA4; worksheet.PageSetup.PrintQuality = 2; //sheet.PageSetup.PrintArea = "A7:T8"; //sheet.PageSetup.PrintTitleRows = "$1:$1"; //worksheet.PageSetup.FitToPagesWide = 1; //worksheet.PageSetup.FitToPagesTall = 1; //worksheet.PageSetup.Orientation = PageOrientationType.Portrait; worksheet.PageSetup.PaperSize = (PaperSizeType)Enum.Parse(typeof(PaperSizeType), comPaperSize.Text); //PrintDialog dialog = new PrintDialog(); //dialog.AllowPrintToFile = true; //dialog.AllowCurrentPage = true; //dialog.AllowSomePages = true; //dialog.AllowSelection = true; //dialog.UseEXDialog = true; //dialog.PrinterSettings.Duplex = Duplex.Simplex; //dialog.PrinterSettings.FromPage = 0; //dialog.PrinterSettings.ToPage = 8; //dialog.PrinterSettings.PrintRange = PrintRange.SomePages; //workbook.PrintDialog = dialog; PrintDocument pd = workbook.PrintDocument; pd.PrinterSettings.Copies = Convert.ToInt16(txtPrintNum.Text.Trim()); //for (int num = 0; num < Convert.ToInt32(txtPrintNum.Text.Trim());num++ ) //{ pd.Print(); //}
/// <summary> /// 字母转阿斯特码 /// </summary> /// <param name="c"></param> /// <returns></returns> private int CharacterToASCI(string c) { byte[] array = new byte[1]; //定义一组数组array array = System.Text.Encoding.ASCII.GetBytes(c); //string转换的字母 int asciicode = (short)(array[0]); return asciicode; } private string ASIToCharacter(int a) { string c = string.Empty; byte[] array = new byte[1]; array[0] = (byte)(Convert.ToInt32(a)); //ASCII码强制转换二进制 c = Convert.ToString(System.Text.Encoding.ASCII.GetString(array)); return c; }