コード例 #1
0
ファイル: CsvForm.cs プロジェクト: yanshi7890/CsvEditor
    public bool SaveFile(string path)
    {
        SaveLayout();

        List <DataGridViewConsoleForm.Message> messageList;

        DataGridViewConsoleForm.Level verifyLevel = VerifierUtility.VerifyWithVerifier(m_Layout.Verifier, m_DataGridView, out messageList);
        DataGridViewConsoleForm.ShowForm(messageList, m_DataGridView, "保存文件");

        bool canSave = false;

        if (verifyLevel == DataGridViewConsoleForm.Level.Info)
        {
            canSave = true;
        }
        else if (verifyLevel == DataGridViewConsoleForm.Level.Warning)
        {
            if (MessageBox.Show("您现在有Warning,确定存储吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                canSave = true;
            }
        }

        if (canSave)
        {
            // 保存文件
            CsvExport myExport = new CsvExport(",", false);
            try
            {
                for (int rowIdx = 0; rowIdx < m_DataTable.Rows.Count; rowIdx++)
                {
                    myExport.AddRow();
                    DataRow dataRow = m_DataTable.Rows[rowIdx];
                    for (int colIdx = 0; colIdx < m_DataTable.Columns.Count; colIdx++)
                    {
                        string value = (string)dataRow[colIdx];
                        myExport[colIdx.ToString()] = value;
                    }
                }

                myExport.ExportToFile(path);
            }
            catch (Exception ex)
            {
                DebugUtility.ShowExceptionMessageBox(string.Format("保存文件({0})失败", path), ex);
                return(false);
            }
            return(true);
        }
        else
        {
            MessageBox.Show(string.Format("保存文件({0})失败", path), "提示");
            return(false);
        }
    }
コード例 #2
0
ファイル: CsvForm.cs プロジェクト: yanshi7890/CsvEditor
    /// <summary>
    /// 移除所有制表符并转换所有换行符
    /// </summary>
    public void RemoveAllTabAndConvertAllLineBreaks()
    {
        BeforeChangeCellValue();

        List <CsvEditManager.CellValueChangeItem> changeList  = new List <CsvEditManager.CellValueChangeItem>();
        List <DataGridViewConsoleForm.Message>    messageList = new List <DataGridViewConsoleForm.Message>();

        for (int rowIdx = 0; rowIdx < m_DataGridView.Rows.Count; rowIdx++)
        {
            DataGridViewRow iterRow = m_DataGridView.Rows[rowIdx];
            for (int colIdx = 0; colIdx < iterRow.Cells.Count; colIdx++)
            {
                DataGridViewCell iterCell  = iterRow.Cells[colIdx];
                string           iterValue = (string)iterCell.Value;
                if (!string.IsNullOrEmpty(iterValue))
                {
                    string newValue = iterValue.Replace("\t", "");
                    newValue = newValue.Replace("\r\n", "\n");

                    if (iterValue != newValue)
                    {
                        CsvEditManager.CellValueChangeItem change = new CsvEditManager.CellValueChangeItem();
                        change.Row      = rowIdx;
                        change.Column   = colIdx;
                        change.OldValue = iterValue;
                        change.NewValue = newValue;
                        changeList.Add(change);

                        DataGridViewConsoleForm.Message message = new DataGridViewConsoleForm.Message();
                        message.Level = DataGridViewConsoleForm.Level.Info;
                        int indexOfTab = iterValue.IndexOf('\t');
                        if (indexOfTab > 0 && indexOfTab < iterValue.Length - 1)
                        {
                            message.Level = DataGridViewConsoleForm.Level.Warning;
                        }
                        message.Row     = rowIdx;
                        message.Column  = colIdx;
                        message.Caption = "移除制表符并转换换行符";
                        message.Text    = string.Format("源:\n({0})\n转换后:\n({1})", iterValue, newValue);
                        messageList.Add(message);

                        iterCell.Value = newValue;
                    }
                }
            }
        }
        EditManager.DidCellsValueChange(changeList);

        AfterChangeCellValue();

        DataGridViewConsoleForm.ShowForm(messageList, m_DataGridView, "移除所有制表符并转换所有换行符");
        MessageBox.Show(string.Format("移除所有制表符并转换所有换行符完成, 转换了({0})个单元格", messageList.Count), "提示");
    }
コード例 #3
0
 public static void ShowForm(List <Message> messages, DataGridView dataGridView, string formText)
 {
     if (messages == null || messages.Count < 1)
     {
         return;
     }
     else
     {
         DataGridViewConsoleForm form = new DataGridViewConsoleForm(messages, dataGridView);
         form.Text = "控制台 - " + formText;
         form.Show();
     }
 }
コード例 #4
0
 /// <summary>
 /// 显示控制台
 /// </summary>
 /// <param name="messages">控制台内显示的消息</param>
 /// <param name="dataGridView">消息对应的DataGridView控件,用于点击MessageItem跳转到DataGridView</param>
 /// <param name="operateName">操作的名字,用于显示窗口标题</param>
 public static void ShowForm(List <Message> messages, DataGridView dataGridView, string operateName)
 {
     if (messages == null || messages.Count < 1)
     {
         return;
     }
     else
     {
         messages.Sort(SortComparison);
         DataGridViewConsoleForm form = new DataGridViewConsoleForm(messages, dataGridView);
         form.Text = string.Format("控制台 - 操作({0}) - 时间({1})", operateName, DateTime.Now);
         form.Show();
     }
 }
コード例 #5
0
    private void CopyCells(DataGridView dataGridView, Point leftUp, Point rightDown)
    {
        List <DataGridViewConsoleForm.Message> messageList = new List <DataGridViewConsoleForm.Message>();

        // 这里是参考DataGridView.GetClipboardContent()
        DataObject         dataObject = new DataObject();
        string             cellContent = null;
        DataGridViewColumn dataGridViewColumn, nextDataGridViewColumn;
        StringBuilder      sbContent = new StringBuilder(1024);

        int lRowIndex = leftUp.X;
        int uRowIndex = rightDown.X;
        DataGridViewColumn lColumn = dataGridView.Columns[leftUp.Y];
        DataGridViewColumn uColumn = dataGridView.Columns[rightDown.Y];

        Debug.Assert(lRowIndex != -1);
        Debug.Assert(uRowIndex != -1);
        Debug.Assert(lColumn != null);
        Debug.Assert(uColumn != null);
        Debug.Assert(lColumn.Index <= uColumn.Index);
        Debug.Assert(lRowIndex <= uRowIndex);

        // Cycle through the visible rows from lRowIndex to uRowIndex.
        int rowIndex     = lRowIndex;
        int nextRowIndex = -1;

        Debug.Assert(rowIndex != -1);
        while (rowIndex != -1)
        {
            if (rowIndex != uRowIndex)
            {
                nextRowIndex = dataGridView.Rows.GetNextRow(rowIndex, DataGridViewElementStates.Visible);
                Debug.Assert(nextRowIndex != -1);
            }
            else
            {
                nextRowIndex = -1;
            }

            // Cycle through the visible columns from lColumn to uColumn
            dataGridViewColumn = lColumn;
            Debug.Assert(dataGridViewColumn != null);
            while (dataGridViewColumn != null)
            {
                if (dataGridViewColumn != uColumn)
                {
                    nextDataGridViewColumn = dataGridView.Columns.GetNextColumn(dataGridViewColumn, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
                    Debug.Assert(nextDataGridViewColumn != null);
                }
                else
                {
                    nextDataGridViewColumn = null;
                }

                cellContent = (string)dataGridView.Rows.SharedRow(rowIndex).Cells[dataGridViewColumn.Index].Value;

                // 验证单元格内容
                if (!string.IsNullOrEmpty(cellContent))
                {
                    cellContent.Replace("\t", "");

                    if (cellContent[0] == '\"')
                    {
                        DataGridViewConsoleForm.Message message = new DataGridViewConsoleForm.Message();
                        message.Level   = DataGridViewConsoleForm.Level.Warning;
                        message.Row     = rowIndex;
                        message.Column  = dataGridViewColumn.Index;
                        message.Caption = "第一个字符是(\")";
                        message.Text    = "不支持复制到Excel(可以复制,但是可能会串行错行,或两个单元格被合并为一个)";
                        messageList.Add(message);
                    }

                    if (cellContent.Contains("\n"))
                    {
                        cellContent = cellContent.Replace("\r\n", "\n");
                        cellContent = "\"" + cellContent + "\"";
                    }
                }

                if (nextDataGridViewColumn == null)
                {
                    if (nextRowIndex != -1)
                    {
                        cellContent = cellContent + "\r\n";
                    }
                }
                else
                {
                    cellContent = cellContent + "\t";
                }

                sbContent.Append(cellContent);
                dataGridViewColumn = nextDataGridViewColumn;
            }
            rowIndex = nextRowIndex;
        }
#if DEBUG
        string content = sbContent.ToString();
        Clipboard.SetDataObject(content);
#else
        Clipboard.SetDataObject(sbContent.ToString());
#endif
        DataGridViewConsoleForm.ShowForm(messageList, dataGridView, "复制");
    }
コード例 #6
0
    private void Merge(DataTable originalCsv, string[][] changedCsv)
    {
        CsvForm csvForm = MainForm.Instance.GetCsvForm();

        csvForm.BeforeChangeCellValue();

        m_ManyThingList  = new List <CsvEditManager.IUndoRedo>();
        m_CellChangeList = new List <CsvEditManager.CellValueChangeItem>();
        m_MessageList    = new List <DataGridViewConsoleForm.Message>();

        for (int iRowInChangedCSV = 0; iRowInChangedCSV < changedCsv.Length; iRowInChangedCSV++)
        {
            string[] changedDataInRow = changedCsv[iRowInChangedCSV];

            string key = changedDataInRow[0];
            changedDataInRow[0] = key.Trim();
            // 跳过空key
            if (string.IsNullOrWhiteSpace(key))
            {
                continue;
            }
            // 为什么要把Trim的结果赋值回changedDataInRow[0]?
            // 因为changedCSV的key中有可能出现空格或换行符,工具生成的CSV文件中的新增和修改的部分都是直接用changedCSV里的内容,所以需要把changedCSV里的keyTrim一下
            if (key.Trim() == "String ID")
            {
                // 如果key是String ID,则是表头,直接无视
                continue;
            }

            bool existed = false;
            for (int iRowInOriginalCSV = 0; iRowInOriginalCSV < originalCsv.Rows.Count; iRowInOriginalCSV++)
            {
                DataRow originalDataInRow = originalCsv.Rows[iRowInOriginalCSV];
                string  originalKey       = (string)originalDataInRow[0];
                if (originalKey.Trim() == key.Trim())
                {
                    // key前后有空格
                    if (key != key.Trim())
                    {
                        DataGridViewConsoleForm.Message message = new DataGridViewConsoleForm.Message();
                        message.Level   = DataGridViewConsoleForm.Level.Warning;
                        message.Column  = 0;
                        message.Row     = iRowInOriginalCSV;
                        message.Caption = "Key的头尾有空白字符";
                        message.Text    = string.Format("我帮你去除了空格\n去空格前的Key:({0})", key);
                        m_MessageList.Add(message);
                    }

                    existed = true;
                    CopyCsvRowToDataRow(iRowInOriginalCSV, changedDataInRow, originalDataInRow);
                    break;
                }
            }

            if (!existed)
            {
                // 添加新行
                DataRow newRow = originalCsv.NewRow();
                for (int iCell = 0; iCell < changedDataInRow.Length; iCell++)
                {
                    newRow[iCell] = "";
                }
                CsvEditManager.DoAddRowEvent doAddRowEvent = new CsvEditManager.DoAddRowEvent
                {
                    Row = originalCsv.Rows.Count
                };
                m_ManyThingList.Add(doAddRowEvent);

                CopyCsvRowToDataRow(originalCsv.Rows.Count, changedDataInRow, newRow);
                originalCsv.Rows.Add(newRow);
            }
        }

        CsvEditManager.DoCellsValueChangeEvent doCellsValueChangeEvent = new CsvEditManager.DoCellsValueChangeEvent
        {
            ChangeList = m_CellChangeList
        };
        m_ManyThingList.Add(doCellsValueChangeEvent);

        csvForm.EditManager.DidManyThings(m_ManyThingList);
        csvForm.AfterChangeCellValue();
        csvForm.UpdateGridHeader();

        DataGridViewConsoleForm.ShowForm(m_MessageList, csvForm.GetDataGridView(), "本地化合并");
    }
コード例 #7
0
 public DataGridViewConsoleForm.Level VerifySelfAndShowConsole(string consoleTitle)
 {
     DataGridViewConsoleForm.Level verifyLevel = VerifySelf(out List <DataGridViewConsoleForm.Message> messageList);
     DataGridViewConsoleForm.ShowForm(messageList, m_DataGridView, consoleTitle);
     return(verifyLevel);
 }
コード例 #8
0
    /// <summary>
    /// 合并文件
    /// </summary>
    private void MergeCsv(DataTable originalCSV, string[][] amCSV)
    {
        CSVForm csvForm = MainForm.Instance.GetCSVForm();

        csvForm.BeforeChangeCellValue();

        // 做的操作(用于撤销)
        m_DoManyThingList = new List <CSVEditManager.IUndoRedo>();
        m_CellChangeList  = new List <CSVEditManager.CellValueChangeItem>();
        m_MessageList     = new List <DataGridViewConsoleForm.Message>();

        // 遍历AM文件
        for (int iRowInAMCSV = 0; iRowInAMCSV < amCSV.Length; iRowInAMCSV++)
        {
            string[] iterAMRow = amCSV[iRowInAMCSV];
            // 这里Trim是为了匹配到源文件中的Key(Key头尾肯定不能用空白字符)
            string iterAMKey = iterAMRow[0].Trim();

            // 跳过空key,Key为空代表这行为空行
            if (string.IsNullOrWhiteSpace(iterAMKey))
            {
                continue;
            }
            // 如果key是String ID,是表头,直接无视
            if (iterAMKey.ToLower() == "String ID".ToLower())
            {
                continue;
            }
            // key中不能包含空格,跳过
            if (iterAMKey.Contains(" "))
            {
                DataGridViewConsoleForm.Message message = new DataGridViewConsoleForm.Message
                {
                    Level   = DataGridViewConsoleForm.Level.Warning,
                    Column  = 0,
                    Row     = iRowInAMCSV,
                    Caption = CSVEditor.Properties.Resources.MergeLocalizationToolAMCSVKeyContainsSpaceMessageCaption,
                    Text    = string.Format(CSVEditor.Properties.Resources.MergeLocalizationToolAMCSVKeyContainsSpaceMessageText, iterAMKey)
                };
                m_MessageList.Add(message);
                continue;
            }

            // 源文件中是否存在AM文件中的Key
            // 存在的话直接merge,不存在的话,把这一行添加到源文件结尾
            bool amKeyExistedInOriginal = false;
            for (int iRowInOriginalCSV = 0; iRowInOriginalCSV < originalCSV.Rows.Count; iRowInOriginalCSV++)
            {
                DataRow iterOriginalRow = originalCSV.Rows[iRowInOriginalCSV];
                string  iterOriginalKey = (string)iterOriginalRow[0];
                if (iterOriginalKey.Trim() == iterAMKey)
                {
                    amKeyExistedInOriginal = true;
                    MergeRow(iRowInOriginalCSV, iterAMRow, iterOriginalRow);
                    break;
                }
            }

            if (!amKeyExistedInOriginal)
            {
                // 添加新行
                DataRow newRow = originalCSV.NewRow();

                CSVEditManager.DoAddRowEvent doAddRowEvent = new CSVEditManager.DoAddRowEvent
                {
                    Row = originalCSV.Rows.Count
                };
                m_DoManyThingList.Add(doAddRowEvent);

                // Key
                newRow[0] = iterAMKey;
                CSVEditManager.CellValueChangeItem changeItem = new CSVEditManager.CellValueChangeItem
                {
                    Row      = originalCSV.Rows.Count,
                    Column   = 0,
                    OldValue = "",
                    NewValue = iterAMKey
                };
                m_CellChangeList.Add(changeItem);

                // 初始化值
                for (int iCell = 1; iCell < iterAMRow.Length; iCell++)
                {
                    newRow[iCell] = "";
                }

                MergeRow(originalCSV.Rows.Count, iterAMRow, newRow, true);
                originalCSV.Rows.Add(newRow);
            }
        }

        CSVEditManager.DoCellsValueChangeEvent doCellsValueChangeEvent = new CSVEditManager.DoCellsValueChangeEvent
        {
            ChangeList = m_CellChangeList
        };
        m_DoManyThingList.Add(doCellsValueChangeEvent);

        csvForm.EditManager.DidManyThings(m_DoManyThingList);
        csvForm.AfterChangeCellValue();
        csvForm.UpdateGridHeader();

        DataGridViewConsoleForm.ShowForm(m_MessageList, csvForm.GetDataGridView(), "本地化合并");
        Close();
    }