C# 合并Excel 设置列单元格格式为"文本格式"

.Net技术 码拜 7年前 (2015-11-26) 1213次浏览
现在合并了两个Excel表格 有一列为文本格式 “0001”通过程序合并后变为“1” 了.
希望还保持“0001”该怎么修改程序?
希望可以实现保留原单元格格式合并.

private void buttonStart_Click(object sender, EventArgs e)
        {
            textBoxInput.ReadOnly = true;
            buttonStart.Enabled = false;
            textBoxInput.Refresh();
            buttonStart.Refresh();
            #region 更新导入列表
            listReflash();
            #endregion
            string[] sheetWords = SplitWords(textBoxSheets.Text);
            int sheetStart=0;
            int sheetEnd=0;
            string[] rangeColumnWords = SplitWords(textBoxRC.Text);
            string[] rangeRowWords = SplitWords(textBoxRR.Text);
            int[,,] rangeRegion = new int[2, 2, 2];
            string[] rangeString = new string[2];
            int standardColumn = Convert.ToInt32(textBoxSC.Text);
            int standardRow = Convert.ToInt32(textBoxSR.Text);
            int fileCount = textBoxInput.Lines.Length;
            if (fileCount < 2)
            {
                MessageBox.Show("导入表至少含有2个文件!");
                return;
            }
            ClassExcelOperation excel = new ClassExcelOperation(2, false, false, false);
            int[] WorkSheetCount = new int[2];
            excel.OpenExcel(textBoxInput.Lines[0], 0);
            WorkSheetCount[0] = excel.WSheetCount(0);
            for (int fileIndex = 1; fileIndex < fileCount; fileIndex++)
            {
                excel.OpenExcel(textBoxInput.Lines[fileIndex], 1);
                WorkSheetCount[1] = excel.WSheetCount(1);
                #region 工作表范围确定sheetStart,sheetEnd
                sheetStart=Convert.ToInt32(sheetWords[0]);
                if (sheetWords[1] == "#")
                {
                    if (WorkSheetCount[0] != WorkSheetCount[1])
                    {
                        string MsgWords;
                        MsgWords = "两个工作薄表单数不一致,将以短表为准!位置:\r\n" + textBoxInput.Lines[0];
                        MsgWords += "\r\n" + textBoxInput.Lines[fileIndex] + "\r\n继续么?";
                        if (MessageBox.Show(MsgWords, "重要提示:", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)
                        {
                            return;
                        }
                        else
                        {
                            sheetEnd = Math.Min(excel.WSheetCount(0), excel.WSheetCount(1));
                        }
                    }
                    else
                    {
                        sheetEnd = excel.WSheetCount(1);
                    }
                }
                else
                {
                    sheetEnd = Convert.ToInt32(sheetWords[1]);
                }
                #endregion
                for (int sheetIndex = sheetStart; sheetIndex <= sheetEnd; sheetIndex++)
                {
                    excel.OperSheet(sheetIndex, 0);
                    excel.OperSheet(sheetIndex, 1); 
                    #region //源操作区范围确定rangeRegion[Point,Way,源]
                    rangeRegion[0, 0, 1] = Convert.ToInt32(rangeRowWords[0]);
                    rangeRegion[0, 1, 1] = Convert.ToInt32(rangeColumnWords[0]);
                    if (rangeRowWords[1] == "#")
                    {
                        rangeRegion[1, 0, 1] = excel.RowCount(1, standardColumn);
                    }
                    else
                    {
                        rangeRegion[1, 0, 1] = Convert.ToInt32(rangeRowWords[1]);
                    }
                    if (rangeColumnWords[1] == "#")
                    {
                        rangeRegion[1, 1, 1] = excel.ColCount(1, standardRow);
                    }
                    else
                    {
                        rangeRegion[1, 1, 1] = Convert.ToInt32(rangeColumnWords[1]);
                    }
                    #endregion
                    if (rangeRegion[0, 0, 1] > rangeRegion[1, 0, 1] | rangeRegion[0, 1, 1] > rangeRegion[1, 1, 1])
                    {
                        #region 空区范围处理
                        if (checkBox2.Checked)
                        {
                            textBoxMsg.Text += "[提示]\r\nfileIndex:" + Convert.ToString(fileIndex);
                            textBoxMsg.Text += " | sheetIndex:" + Convert.ToString(sheetIndex) + "为空记录\r\n";
                            textBoxMsg.Refresh();
                        }
                        #endregion
                    }
                    else
                    {
                        #region //目标操作区范围确定rangeRegion[Point,Way,目标]
                        rangeRegion[0, 0, 0] = excel.RowCount(0, standardColumn) + 1;
                        rangeRegion[0, 1, 0] = rangeRegion[0, 1, 1];
                        rangeRegion[1, 0, 0] = rangeRegion[0, 0, 0] + rangeRegion[1, 0, 1] - rangeRegion[0, 0, 1];
                        rangeRegion[1, 1, 0] = rangeRegion[1, 1, 1];
                        #endregion
                        #region //操作区格式化
                        rangeString[0] = excel.Tools_NtoC(rangeRegion[0, 1, 0]) + Convert.ToString(rangeRegion[0, 0, 0]) + ":";
                        rangeString[0] += excel.Tools_NtoC(rangeRegion[1, 1, 0]) + Convert.ToString(rangeRegion[1, 0, 0]);
                        rangeString[1] = excel.Tools_NtoC(rangeRegion[0, 1, 1]) + Convert.ToString(rangeRegion[0, 0, 1]) + ":";
                        rangeString[1] += excel.Tools_NtoC(rangeRegion[1, 1, 1]) + Convert.ToString(rangeRegion[1, 0, 1]);
                        #endregion
                        Excel.Range ran = excel.get_Range(rangeString[1], 1);
                        excel.set_Range(rangeString[0], ran, 0);
                    }
                }
                textBoxMsg.Text += "[提示]\r\n  fileIndex:" + Convert.ToString(fileIndex);
                textBoxMsg.Text += " | 导入成功!\r\n";
                textBoxMsg.Refresh();
                excel.CloseExcel(1);
            }
            textBoxMsg.Text += "[提示]\r\n  fileIndex:0 | 导入成功!\r\n";
            textBoxMsg.Refresh();
            excel.SaveAs(textBoxOutput.Text, 0);
            excel.CloseExcel(0);
            excel.CloseApp();
            textBoxMsg.Text += "[全部完成]\r\n  " + textBoxOutput.Text + "保存成功!";
            textBoxMsg.Refresh();
            textBoxInput.ReadOnly = false;
            buttonStart.Enabled = true;
解决方案:40分
前面加 “”   单引号

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C# 合并Excel 设置列单元格格式为"文本格式"
喜欢 (0)
[1034331897@qq.com]
分享 (0)