/// <summary> /// 分割方法 /// </summary> /// <param name="obj"></param> private void SplitFileThread(object obj) { FileSplitModel fileSplitModel = obj as FileSplitModel; //文件判断 string outFile = @fileSplitModel.fileSrc; if (File.Exists(outFile)) { File.Delete(outFile); } if (!System.IO.File.Exists(outFile)) { FileStream fs; fs = File.Create(outFile); fs.Close(); } //新建文件流按照指定的块数分割文件 using (FileStream newFileStream = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write)) { int fileData = 0; int bufferLength = Convert.ToInt32(fileSplitModel.fileLength); //指定byte数组大小 byte[] buffer = new byte[bufferLength]; //读取指定大小的流 if ((fileData = fileStream.Read(buffer, fileSplitModel.fileStreamReadOffset, buffer.Length)) > 0) { //转换为默认编码的流 BinaryWriter bw = new BinaryWriter(newFileStream, Encoding.UTF8); if (cb_CustomContent.Checked && !string.IsNullOrWhiteSpace(txt_CustomContent.Text)) { bw.Write(txt_CustomContent.Text.Trim()); } //将字节数组写入流 bw.Write(buffer, 0, fileData); bw.Flush(); bw.Dispose(); bw.Close(); } else { ShowMessage.ShowError(string.Format(ChangeLanguage.getLanguage("SplitPrompt"), outFile), showTitle); return; } if (fileSplitModel.fileIndex == -1) { ShowMessage.ShowContent(ChangeLanguage.getLanguage("ExecutionCompleted"), showTitle); } } }
private void btn_Split_Click(object sender, EventArgs e) { if (!RequiredVerification()) { return; } if (ShowMessage.ShowConfirm(ChangeLanguage.getLanguage("ConfirmExecution"), showTitle) == DialogResult.OK) { FileSplitModel splitModel = new FileSplitModel(); splitModel.filePath = txt_SelectFile.Text.Trim(); splitModel.fileOutPath = txt_Src.Text.Trim(); splitModel.fileName = txt_FileName.Text.Trim(); splitModel.fileExtension = splitModel.filePath.Substring(splitModel.filePath.LastIndexOf('.')); if (cb_WaterMark.Checked) { WaterMarkModel waterModel = new WaterMarkModel(); waterModel.waterMarkContent = txt_WaterMark.Text.Trim(); waterModel.waterMarkColor = txt_Color.Text.Trim(); waterModel.waterMarkFontSize = float.Parse(txt_FontSize.Text.Trim()); waterModel.waterMarkLayout = int.Parse(cbBox_Layout.SelectedValue.ToString()); splitModel.fileWaterMark = waterModel; } if (rbtn_SectionBreak.Checked)//分节符 { try { SplitSectionMethodInvoker splitSectionMethod = new SplitSectionMethodInvoker(SplitSectionBreak); this.BeginInvoke(splitSectionMethod, new object[] { splitModel }); } catch (Exception ex) { ShowMessage.ShowError(ex.Message, showTitle); } } else if (rbtn_PageBreak.Checked)//分页符 { try { SplitPageMethodInvoker splitPageMethod = new SplitPageMethodInvoker(SplitPageBreak); this.BeginInvoke(splitPageMethod, new object[] { splitModel }); } catch (Exception ex) { ShowMessage.ShowError(ex.Message, showTitle); } } } }
/// <summary> /// 根据分节符进行分割方法 /// </summary> /// <param name="obj"></param> private void SplitSectionBreak(object obj) { try { FileSplitModel splitModel = obj as FileSplitModel; //实例化Document对象 Document doc = new Document(); //载入待拆分的Word文档 doc.LoadFromFile(splitModel.filePath); Document newWord; int index = 0; foreach (Section item in doc.Sections) { newWord = new Document(); //每有一个section就创建一个新的文档 newWord.Sections.Add(item.Clone()); //复制section内容到新文档 if (splitModel.fileWaterMark != null) { TextWatermark txtWatermark = new TextWatermark();//新建一个TextWatermark对象,设置文本水印字样 txtWatermark.Text = splitModel.fileWaterMark.waterMarkContent; //设置文本水印字体大小、颜色和文本排列方式 txtWatermark.FontSize = splitModel.fileWaterMark.waterMarkFontSize; txtWatermark.Color = Color.FromName(splitModel.fileWaterMark.waterMarkColor); if (splitModel.fileWaterMark.waterMarkLayout == 0) { txtWatermark.Layout = WatermarkLayout.Diagonal; } else { txtWatermark.Layout = WatermarkLayout.Horizontal; } newWord.Watermark = txtWatermark; //将文本应用到Word文档水印 } newWord.SaveToFile(splitModel.fileOutPath + @"\" + splitModel.fileName + index + splitModel.fileExtension, FileFormat.Auto); //保存文档 index++; } ShowMessage.ShowInfomation(ChangeLanguage.getLanguage("ExecutionCompleted"), showTitle); } catch (Exception ex) { ShowMessage.ShowError(ex.Message, showTitle); } }
private void btn_Split_Click(object sender, EventArgs e) { if (!RequiredVerification()) { return; } if (ShowMessage.ShowConfirm(ChangeLanguage.getLanguage("ConfirmExecution"), showTitle) == DialogResult.OK) { FileSplitModel fileSplitModel = new FileSplitModel(); fileSplitModel.filePath = txt_SelectFile.Text.Trim(); fileSplitModel.fileOutPath = txt_Src.Text.Trim(); fileSplitModel.fileName = txt_FileName.Text.Trim(); fileSplitModel.fileExtension = fileSplitModel.filePath.Substring(fileSplitModel.filePath.LastIndexOf('.')); ExcelSplitModel excelModel = new ExcelSplitModel(); if (rbtn_AllWorkBook.Checked) { excelModel.splitType = 1; } else if (rbtn_FixedRows.Checked) { excelModel.splitType = 2; excelModel.sheetName = ""; excelModel.beginRow = int.Parse(txt_FixedRowsBegin.Text.Trim()); excelModel.endRow = int.Parse(txt_FixedRowsEnd.Text.Trim()); if (rbtn_FixedRowsDesignation.Checked) { ComboBoxModel model = cbBox_FixedRowsWorkSheet.SelectedItem as ComboBoxModel; if (model == null) { ShowMessage.ShowError(ChangeLanguage.getLanguage("SpecifiedWorksheetCannotBeEmpty"), showTitle); return; } excelModel.sheetName = model.text; } } fileSplitModel.fileExcel = excelModel; SplitMethodInvoker splitAllMethod = new SplitMethodInvoker(SplitSheetMethod); this.BeginInvoke(splitAllMethod, new object[] { fileSplitModel }); } }
/// <summary> /// 根据分页符进行分割方法 /// </summary> /// <param name="obj"></param> private void SplitPageBreak(object obj) { try { FileSplitModel splitModel = obj as FileSplitModel; Document doc = new Document(); doc.LoadFromFile(splitModel.filePath);//载入待拆分的Word文档 //实例化一个新的文档并添加新章节 Document newWord = new Document(); Section section = newWord.AddSection(); int index = 0; //根据章节,段落的层次由大到小依次遍历文档元素,复制内容到新的文档 foreach (Section sec in doc.Sections) { foreach (DocumentObject docObj in sec.Body.ChildObjects) { if (docObj is Paragraph) { Paragraph para = docObj as Paragraph; section.Body.ChildObjects.Add(para.Clone()); foreach (DocumentObject parobj in para.ChildObjects) { //找到段落中的分页符,保存到新文档 if (parobj is Break && (parobj as Break).BreakType == BreakType.PageBreak) { int i = para.ChildObjects.IndexOf(parobj); section.Body.LastParagraph.ChildObjects.RemoveAt(i); if (splitModel.fileWaterMark != null) { //新建文本水印 TextWatermark txtWatermark = new TextWatermark(); txtWatermark.Text = splitModel.fileWaterMark.waterMarkContent; txtWatermark.FontSize = splitModel.fileWaterMark.waterMarkFontSize; txtWatermark.Color = Color.FromName(splitModel.fileWaterMark.waterMarkColor); if (splitModel.fileWaterMark.waterMarkLayout == 0) { txtWatermark.Layout = WatermarkLayout.Diagonal; } else { txtWatermark.Layout = WatermarkLayout.Horizontal; } newWord.Watermark = txtWatermark;//将文本应用到Word文档水印 } newWord.SaveToFile(splitModel.fileOutPath + @"\" + splitModel.fileName + index + splitModel.fileExtension, FileFormat.Auto); index++; //一个文档完成之后新建一个文档 newWord = new Document(); section = newWord.AddSection(); //复制上一个分页符所在的段落的所有内容到新文档 section.Body.ChildObjects.Add(para.Clone()); //如果新文档第一段(也就是刚刚复制的那一段)没有子元素,则把文档的第一个子元素删除 if (section.Paragraphs[0].ChildObjects.Count == 0) { section.Body.ChildObjects.RemoveAt(0); } else { //如果有内容则删除分页符之前的所有内容 while (i >= 0) { section.Paragraphs[0].ChildObjects.RemoveAt(i); i--; } } } } } if (docObj is Table) { section.Body.ChildObjects.Add(docObj.Clone()); } } } if (splitModel.fileWaterMark != null) { //新建文本水印 TextWatermark txtWatermark = new TextWatermark(); txtWatermark.Text = splitModel.fileWaterMark.waterMarkContent; txtWatermark.FontSize = splitModel.fileWaterMark.waterMarkFontSize; txtWatermark.Color = Color.FromName(splitModel.fileWaterMark.waterMarkColor); if (splitModel.fileWaterMark.waterMarkLayout == 0) { txtWatermark.Layout = WatermarkLayout.Diagonal; } else { txtWatermark.Layout = WatermarkLayout.Horizontal; } newWord.Watermark = txtWatermark;//将文本应用到Word文档水印 } newWord.SaveToFile(splitModel.fileOutPath + @"\" + splitModel.fileName + index + splitModel.fileExtension, FileFormat.Auto); ShowMessage.ShowInfomation(ChangeLanguage.getLanguage("ExecutionCompleted"), showTitle); } catch (Exception ex) { ShowMessage.ShowError(ex.Message, showTitle); } }
private void SplitSheetMethod(object obj) { FileSplitModel fileSplitModel = obj as FileSplitModel; Workbook workBook = new Workbook(); try { workBook.LoadFromFile(fileSplitModel.filePath); foreach (Worksheet sheet in workBook.Worksheets) { //实例化一个新的Workbook对象,创建一个空的工作表 Workbook newWorkBook = new Workbook(); newWorkBook.CreateEmptySheets(1); string filePath = fileSplitModel.fileOutPath + @"\" + fileSplitModel.fileName + sheet.Index + fileSplitModel.fileExtension; //拆分 if (fileSplitModel.fileExcel.splitType == 1) { if (fileSplitModel.fileExtension == ".xls") { newWorkBook.SaveToFile(filePath, ExcelVersion.Version97to2003); } else { newWorkBook.SaveToFile(filePath, ExcelVersion.Version2007); } newWorkBook.LoadFromFile(filePath);//加载工作簿 //获取源excel工作表中的工作表,将它们复制到newWorkBook中的工作表 Worksheet newSheet = newWorkBook.Worksheets.AddCopy(sheet); newWorkBook.Worksheets.RemoveAt(0); newSheet.Name = sheet.Name; } else if (fileSplitModel.fileExcel.splitType == 2) { if (sheet.FirstRow == -1) { continue; } string sheetName = fileSplitModel.fileExcel.sheetName; int begin = fileSplitModel.fileExcel.beginRow; int end = fileSplitModel.fileExcel.endRow; if (end == 0) { end = sheet.LastRow; } Worksheet newWorkSheet = newWorkBook.Worksheets[0]; CellRange range = sheet.Range[begin, 1, end, sheet.LastColumn]; if (!string.IsNullOrWhiteSpace(sheetName)) { if (sheet.Name == sheetName) { newWorkSheet.Copy(range, newWorkSheet.Range[1, 1]); } } else { newWorkSheet.Copy(range, newWorkSheet.Range[1, 1]); } newWorkSheet.Name = sheet.Name; } if (fileSplitModel.fileExtension == ".xls") { newWorkBook.SaveToFile(filePath, ExcelVersion.Version97to2003); } else { newWorkBook.SaveToFile(filePath, ExcelVersion.Version2007); } newWorkBook.Dispose(); } workBook.Dispose(); ShowMessage.ShowContent(ChangeLanguage.getLanguage("ExecutionCompleted"), showTitle); } catch (Exception ex) { workBook.Dispose(); ShowMessage.ShowError(ex.Message, showTitle); } }
private void btnSplit_Click(object sender, EventArgs e) { if (!RequiredVerification()) { return; } if (ShowMessage.ShowConfirm(ChangeLanguage.getLanguage("ConfirmExecution"), showTitle) == DialogResult.OK) { string filePath = txt_SelectFile.Text.Trim(); if (!File.Exists(filePath))//文件不存在 { ShowMessage.ShowError(ChangeLanguage.getLanguage("FilePathError"), showTitle); return; } //新建文件流 if (fileStream != null) { fileStream.Close(); } fileStream = new FileStream(txt_SelectFile.Text, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //设置当前流的位置,即开始位置 fileStream.Seek(0, SeekOrigin.Begin); try { //获取文件信息 double fixSize = 0; int fileCount = 0; if (rbtn_Size.Checked)//固定文件大小 { fixSize = double.Parse(txt_FixedSize.Text); fileCount = Convert.ToInt32(txt_Total.Text); } else if (rbtn_Total.Checked)//固定分割份数 { fixSize = double.Parse(txt_Size.Text); fileCount = Convert.ToInt32(txt_FixedTotal.Text); } for (int i = 0; i < fileCount; i++) { //实例化分割文件信息 FileSplitModel splitModel = new FileSplitModel(); splitModel.fileName = txt_FileName.Text + i.ToString(); splitModel.fileExtension = ".txt"; splitModel.fileOutPath = txt_Src.Text; splitModel.fileSrc = splitModel.fileOutPath + @"\" + splitModel.fileName + splitModel.fileExtension; splitModel.fileLength = Convert.ToInt32(fixSize).ToString(); splitModel.fileStreamReadOffset = 0; if (i == fileCount - 1) { splitModel.fileIndex = -1; } else { splitModel.fileIndex = i; } SplitMethodInvoker splitMethod = new SplitMethodInvoker(SplitFileThread); this.BeginInvoke(splitMethod, new object[] { splitModel }); } } catch (Exception ex) { ShowMessage.ShowError(ex.Message, showTitle); } } }