当前位置:网站首页>[C # note] the data in DataGridView saved in WinForm is excel and CSV
[C # note] the data in DataGridView saved in WinForm is excel and CSV
2022-07-02 07:52:00 【Silent clouds】
Save as Excel
Use system.io preservation , Libraries that need to be imported :using System.IO;
// Use system.io preservation
//grid For the whole DataGridView object , and filePath For the path to save ( Include suffix ), The same below
public void saveExcelIO(DataGridView grid, string filePath)
{
Thread.Sleep(1000);
StreamWriter sw = new StreamWriter(filePath, false, Encoding.GetEncoding("gb2312"));
StringBuilder sb = new StringBuilder();
// Write the title
for(int k = 0; k < grid.Columns.Count; k++)
{
if (grid.Columns[k].Visible)
{
//\t It's equivalent to typing one on the keyboard tab key
sb.Append(grid.Columns[k].HeaderText.ToString().Trim() + "\t");
}
}
// Line break
sb.Append(Environment.NewLine);
// Start writing values for each line
for (int i = 0; i < grid.Rows.Count - 1; i++)
{
System.Windows.Forms.Application.DoEvents();
for(int j = 0; j < grid.Columns.Count; j++)
{
if (grid.Columns[j].Visible)
{
// Cells have a certain number of bytes , May exceed , If it exceeds the limit, the contents of two cells will be the same
sb.Append(grid.Rows[i].Cells[j].Value.ToString().Trim() + "\t");
}
}
sb.Append(Environment.NewLine);// Line break
}
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
}
Save as CSV
// preservation csv
public void saveCSV(DataGridView grid,string filePath)
{
Thread.Sleep(1000);
StreamWriter sw = new StreamWriter(filePath, false, Encoding.GetEncoding("gb2312"));
string strLine = "";
// Header
for (int i = 0; i < grid.ColumnCount; i++)
{
if (i > 0)
strLine += ",";
strLine += grid.Columns[i].HeaderText;
}
strLine.Remove(strLine.Length - 1);
sw.WriteLine(strLine);
strLine = "";
for(int j = 0; j < grid.Rows.Count; j++)
{
strLine = "";
int col = grid.Columns.Count;
for(int k = 0; k < col; k++)
{
if (k > 0 && k < col)
strLine += ",";
if (grid.Rows[j].Cells[k].Value == null)
strLine += "";
else
{
string cell = grid.Rows[j].Cells[k].Value.ToString().Trim();
// Prevent special symbols inside
cell = cell.Replace("\"", "\"\"");
cell = "\"" + cell + "\"";
strLine += cell;
}
}
sw.WriteLine(strLine);
}
sw.Close();
}
边栏推荐
- Two dimensional array de duplication in PHP
- Solve the problem of latex picture floating
- Machine learning theory learning: perceptron
- How to turn on night mode on laptop
- 【MagNet】《Progressive Semantic Segmentation》
- 【TCDCN】《Facial landmark detection by deep multi-task learning》
- Implementation of yolov5 single image detection based on pytorch
- Mmdetection model fine tuning
- Deep learning classification Optimization Practice
- [binocular vision] binocular correction
猜你喜欢

Semi supervised mixpatch

ModuleNotFoundError: No module named ‘pytest‘

【Wing Loss】《Wing Loss for Robust Facial Landmark Localisation with Convolutional Neural Networks》

【Batch】learning notes

浅谈深度学习模型中的后门

Record of problems in the construction process of IOD and detectron2

深度学习分类优化实战

Implementation of yolov5 single image detection based on pytorch

open3d学习笔记四【表面重建】

MMDetection安装问题
随机推荐
【MobileNet V3】《Searching for MobileNetV3》
Faster-ILOD、maskrcnn_ Benchmark trains its own VOC data set and problem summary
【C#笔记】winform中保存DataGridView中的数据为Excel和CSV
【学习笔记】Matlab自编图像卷积函数
利用Transformer来进行目标检测和语义分割
Thesis tips
自然辩证辨析题整理
latex公式正体和斜体
conda常用命令
What if the notebook computer cannot run the CMD command
open3d学习笔记三【采样与体素化】
【Sparse-to-Dense】《Sparse-to-Dense:Depth Prediction from Sparse Depth Samples and a Single Image》
MMDetection安装问题
[binocular vision] binocular stereo matching
【双目视觉】双目矫正
【Cutout】《Improved Regularization of Convolutional Neural Networks with Cutout》
解决latex图片浮动的问题
【MagNet】《Progressive Semantic Segmentation》
半监督之mixmatch
【Random Erasing】《Random Erasing Data Augmentation》